Exception Handling
Exception Handling
Exception Hierarchy
Exception: An unwanted unexpected event that disturbs normal flow of the program is
called exception.
Types of Exceptions:
1. Checked Exceptions:
Checked exceptions are those that the compiler forces you to handle explicitly.
2. Unchecked Exceptions:
Unchecked exceptions are not checked at compile-time but are thrown at runtime.
Note: RuntimeException and its child classes, Error and its child classes are unchecked
runtime only and there is no chance of occurring any exception at compile time
A checked exception is said to be fully checked if and only if all its child classes are also checked.
Example:
1) IOException
2) InterruptedException
A checked exception is said to be partially checked if and only if some of its child classes are
unchecked.
Example:
Exception
1. Throwable.
2. Exception.
try {
}
Note:
1. Within the try block if anywhere an exception raised then rest of the try block won't be executed
even though we handled that exception. Hence, we have to place/take only risk code inside try block
and length of the try block should be as less as possible.
2. If any statement which raises an exception and it is not part of any try block then it is always
abnormal termination of the program.
3. There may be a chance of raising an exception inside catch and finally blocks also in addition to try
block.
1. printStackTrace():
This method prints exception information in the following format. Name of the
exception: description of exception
3. getMessage():
The way of handling an exception is varied from exception to exception. Hence for every exception
type it is recommended to take a separate catch block. That is try with multiple catch blocks is
possible and recommended to use.
EXAMPLE:
try {
return result;
Finally block:
1. It is not recommended to take clean up code inside try block because there is no guarantee
for the execution of every statement inside a try.
2. It is not recommended to place clean up code inside catch block because if there is no
exception then catch block won't be executed.
3. We require some place to maintain clean up code which should be executed always
irrespective of whether exception raised or not raised and whether handled or not handled.
Such type of best place is nothing but finally block.
4. Hence the main objective of finally block is to maintain cleanup code.
try {
try
{
System.out.println("try block executed");
}
catch(ArithmeticException e)
{
System.out.println("catch block executed");
}
finally
{
System.out.println("finally block executed");
}
Output:
try block executed
Finally block executed
try
{
System.out.println("try block executed");
System.out.println(10/0);
}
catch(ArithmeticException e)
{
System.out.println("catch block executed");
}
finally
{
System.out.println("finally block executed");
}
Output:
Try block executed
Catch block executed
Finally block executed
return Vs finally:
Even though return statement presents in try or catch blocks first finally will be executed and after
that only return statement will be considered. finally block dominates return statement.
try
{
System.out.println("try block executed");
return;
}
catch(ArithmeticException e)
{
System.out.println("catch block executed");
}
finally
{
System.out.println("finally block executed");
}
Output:
try block executed
Finally block executed
If return statement present try, catch and finally blocks then finally block return statement will be
considered.
Example:
try
{
System.out.println(10/0);
return 777;
}
catch(ArithmeticException e)
{
return 888;
}
finally{
return 999;
}
}
Output:
999
Note:
finally vs System.exit(0):
There is only one situation where the finally block won't be executed is whenever we are using
System.exit(0) method. Whenever we are using System.exit(0) then JVM itself will be shutdown , in
this case finally block won't be executed.
final:
finally:
finally is the block always associated with try-catch to maintain clean up code which should be
executed always irrespective of whether exception raised or not raised and whether handled or not
handled.
finalize:
finalize is a method, always invoked by Garbage Collector just before destroying an object to perform
cleanup activities.
throw statement:
In Java, the throws clause is used in a method signature to indicate that the method might throw
certain types of exceptions during its execution. When a method is declared with a throws clause, it
means that the responsibility of handling those exceptions is passed to the caller of the method.
throw statement:
he throw statement in Java is used to explicitly throw an exception. It is typically used in methods to
indicate that a specific exceptional condition has occurred, and the normal flow of the program
should be interrupted.