Exception Handling in JAVA
Exception Handling:-
- Each and every exception is considered as an object.
- If there is any exception in running programming then program receives appropriate object or an exception from JRE.
- After receiving an object of that exception it is responsibility of the program to handle that exception.
- If the program is unable to handle an exception then that exception is forwarded to the default exception handle in java.
- Default exception handler of java prints an exception message on the screen and terminates the program abnormally.
- To avoid this abnormal termination we have to handle that exception in our program.
- After handling an exception in the program our program can continue for remaining instructions.
e.g.
void gun()
{
int no;
no=10/0;
}
void fun()
{
gun();
}
public static void main(String args[])
{
fun();
}
Output:-
gun :: ArithmaticException, divideByZeroException
fun
main
- This is a stack trace of our program. In this program we get an exception inside a gun function and JRE prints each and every function call from that function by unwinding the stack frame.
- In this case exception is not handled due to which it is forwarded to the default exception handler.
Try block:-
- All the statements written inside a try block are observed by the JRE for the exceptions.
- If there is some exception inside a try then JRE throws an object of that exception and that object is accepted by appropriate catch block.
- Inside a catch block we have to write a code for handling that exception.
- After handling the exception inside the catch block our program can continue its exception successfully.
- If there is no appropriate catch block inside the program then that exception is forwarded to its super class exception catch code and default exception handler of java.
e.g.
class demo
{
int i,j,k;
i=10;
j=0;
fun()
{
try{
System.out.println(“inside a try block”);
k=i/j;
}catch(ArithmaticException e)
{
System.out.println(“inside catch block.”);
}
System.out.println(“After the catch block.”);
}
}
class hello
{
public static void main(String args[])
{
demo d = new demo();
d.fun();
System.out.println(“after calling fun.”);
}
}
Output:-
inside try block
inside catch block
after the catch block
after calling fun
- If the try block contains an exception code then the code after exceptional instruction is not executed because the control is directly transfer to the catch block.
- Therefore the line after exceptional instruction is not executed.
Multiple catch or single try block:-
- We can write multiple catch blocks for single try block.
- The appropriate catch block is evaluated depend on the exception.
- If we have to provide catch block then the flow of that catch block must be in decreasing order of their class hierarchy.
- Means a sub class exception is handled first before derived class exception.
- If there is an exception inside a catch block then JRE scans each and every catch block in the top to bottom order.
- If any catch block is matched with that exception then control is not passed by remaining catch block.
e.g.
try{
int no=10/0;
}catch(Exception e)
{
System.out.println(“inside first catch block.”);
}
catch(ArithmaticException e)
{
System.out.println(“inside second catch block.”);
}
Output:-
Error
- Because in java unreachable code is considered as an error.
- In our example, second catch block is an unreachable code.
- But if we swap hat catch block then program runs successfully.
- Because ArithmaticException catch block can successfully handled divideByZeroException.
For more reading about technology news in singapore and seo to online marketing do view more about other pages.