<<Previous Next>>
- We can handle exceptions explicitly by using try, catch.
- The risky code (Where there may be rising of exceptions) we have to keep in try block and corresponding handling code we have to take inside the catch block.
- If any exception is raised in try block then corresponding catch block will be executed to handle that exception. After execution of catch block the rest of the code will be executed normally and it will become graceful termination of program.
- We will use the try, catch blocks as follows.
try { Risky code; } catch (x e) { Handling code; }Example1: -
class Test{ public static void main(String args[]){ System.out.println("statement 1"); System.out.println(10/0); System.out.println("statement 3"); } }OutPut:
statement 1
Abnormal termination
In the line 4 ArithmeticException exception will come.
Example2: -
class Test{ public static void main(String args[]){ System.out.println("stmt 1"); try{ System.out.println(10/0); }catch(ArithmeticException e){ System.out.println(10/2); } System.out.println("stmt 3"); } }Output:
stmt1
5
Stmt 3
Normal termination
Control Flow in try, catch: -
try { statement1; statement2; statement3; } catch(X e){ statement4; } statement5;
Case1: If there is no
Exception.
1,2,3,5 Normal termination
Case2: If an
exception is raised at statement2 and corresponding catch block has matched.
1, 4, 5 Normal termination
Case3: If an
Exception is raised at statement2 and corresponding catch block has not
matched.
1 Abnormal
termination
Case4: Exception rose at statement4.
It’s always abnormal termination of program.
It’s always abnormal termination of program.
Case5: Exception
rose at statement5.
Abnormal
termination of program.
Try with multiple catch blocks: -
Try with multiple catch blocks: -
The way of handling an exception is
varied from exception to exception, compulsory we have to write the separate
catch block for every exception. Hence
try with multiple catch blocks is possible and it is recommended to use.
try { Risky code; } catch (ArithmeticException e) { Handling code for ArithmeticException; } catch (IOException e) { Handling code for IOException; } catch (NullPointerException e) { Handling code for NullPointerException; } catch (Exception e) { Default exception handling code; }
If try with multiple catch blocks
present then, the order of catch blocks is very important. It should be from child to parent but not
parent to child.
Examples:
Examples:
try{ }catch (ArithmeticException e){ // }catch (Exception e){ // }The above one is correct.
try{ } catch (Exception e){ // } catch (ArithmeticException e){ // }
CE: java.lang.ArithmeticException
has already caught.
Method to display Exception Information:-
Throwable class defines the following methods to print Exception information or Error Information.
1. printStackTrace()
This method prints Exception information in the following format.
Name of Exception: Description
Stack trace
2. toString()
This method prints Exception information in the following format.
Name of Exception: Description
3. getMessage()
This prints only description of Exception.
try { System.out.println (10/0); } catch (ArithmeticException e){ e.printStackTrace (); System.out.println (e); or System.out.println (e.toString ()); System.out.println (e.getMessage ()); }
O/P for the line 4: ArithmeticException: /by zero at main()
O/P for the line 5: ArithmeticException: /by zero
O/P for the line 6: /by zero
Note: Default exception handler always use printStackTrace() method to display
Exception information.
No comments:
Post a Comment