Skip to main content

Posts

Showing posts with the label OCJP

Exception – Handle or Declare Rule

Exception – Handle or Declare Rule Java – Exceptions An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted  and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled. Advantage of Exception Handling The core advantage of exception handling is  to maintain the normal flow of the application . Exception normally disrupts the normal flow of the application that is why we use exception handling. Let’s take a scenario: statement  1 ; statement  2 ; statement  3 ; statement  4 ; statement  5 ; //exception occurs statement  6 ; statement  7 ; statement  8 ; statement  9 ; statement  10 ; Suppose there are 10 statements in your program and there occurs an exception at statement 5, rest of the code will not be executed i...

Exception Hierarchy

Exception Hierarchy What is an Exception An exception is a problem that occurs during the execution of a program and disrupts the normal flow of  the program . Exception Hierarchy All the  checked  exceptions and unchecked exceptions are subclasses of Exception class and the Exception class is subclass of Throwable class as  shown  below. Types of Exceptions There are two types of exceptions in java 1. Checked exceptions 2. Unchecked exceptions 1) Checked Exceptions:  are the exceptions that are checked at compile time. If some code within a method throws a checked exception,  then the method must either handle the exception(using  try-catch  block) or it must specify the exception using  throws  keyword. Else, the code will not compile. 2) Unchecked   Exceptions:  are the exceptions that are not checked at compiled time, so it is not forced by the compiler to either handle or specify the exception...