Exception Handling
Exception Handling
Exception occurs only during runtime and when it occurs, program gets terminated.
How to handle?
If the exception did not occur in try block, catch block will not get executed.
How to handle?
catch block gets executed only if exception happens in the try block and if the
exception matches the catch block.
When exception occurs in the try block, then the control immediately comes out
of try block without executing the remaining lines of code with in the try block
and executes the matching catch block.
If the matching catch block is not found, then the program gets terminated.
Once the control comes out of try block, it will not got to try block again.
There must not be any executable code in between try and catch blocks.
Exception means, jvm creates object of appropriate exception class and that
object is thrown.
Reference type used in the catch block must belong to exception category. That
is, it must be directly or indirectly a sub class of throwable.
try with multiple catch
try
{
… math operation …
… save the results to file …
… save the results to a database …
}
catch(ArithmeticException ae)
{
Handling scenarios
}
catch(IOException ioe)
{
Handling scenarios
}
catch(SQLException sqle)
{
Handling scenarios
}
Nested try and catch blocks
try
{
… math operation …
try {
… save the results to file …
}
catch(IOException ioe)
{
Handling scenarios
}
try {
… save the results to a database …
}
catch(SQLException sqle)
{
Handling scenarios
}
}
catch(ArithmeticException ae)
{
Handling scenarios
}
Nested try and catch blocks
try
{
… math operation …
try {
… save the results to file …
}
catch(IOException ioe)
{
Handling scenarios
}
try {
… save the results to a database …
}
catch(SQLException sqle)
{
Handling scenarios
}
}
catch(ArithmeticException ae)
{
Handling scenarios
}
Points to Remember
If the handling scenario is the same for multiple exceptions, then we write single
catch block.
From jdk 1.7 onwards, we can handle multiple exceptions in a single catch block.
In case of multiple catch blocks, the sequence must always be sub class to super
class.
Heavy weight objects
Those objects which consumes system or network resources.
It is very important to close such resources otherwise, the application becomes slow and
creates performance issues.
How to close?
Scanner sc = new Scanner(System.in);
int c = sc.nextInt();
…. remaining lines of code ….
sc.close();
finally block
Usually costly (heavy weight object) resources are closed inside finally block.
Checked Exception
Those exception for which the compiler checks at the compile time, whether
the exception is handled by the developer inside the code or not.
If the exception is not handled, then the compiler gives an error and force
the developer to handle the exception.
The exception certainly happens only at the runtime but the compiler
checks whether it is handled or not.
Those exception for which the compiler does not checks at the compile
time, whether the exception is handled by the developer inside the code or
not.
If the exception is not handled, then the compiler does not give any error
and will not force the developer to handle the exception.