The general form of try block is:
try { - - - - - -//Risky Code - - - - - - - }
The general form of catch block is
catch( exception_type e) { - - - - - - -//Handling Code - - - - - - - s}
Let us see a program to understand the use of try and catch
class ExceptionHandlingDemo { public static void main(String args[]) { int x=0; int y=25; int z; try { z= y/x; } catch( ArithmeticException e) { system.out.println(“vivtech”); } system.out.println(“viswass”); } }
vivtech
viswass
The JVM firstly checks whether the exception is handled or not. If exception is not handled, JVM provides a default exception handler that performs the following tasks:
viswass
But if exception is handled by the application programmer, normal flow of the application is maintained i.e. rest of the code is executed.
It is possible to write more than one catch block in a program. The general form is as follows
try { Statement; } catch( exception-type1 e) { Statement; } catch( exception-type2 e) { Statement; } _ _ _ _ _ _ _ _ _ _ _ _ _ _ catch( exception-typeN e) { Statement; } _ _ _ _ _ _ _ _ _ _ _ _ _ _
When an exception is found in try a block java encounters multiple catch blocks like cases in switch statement that means the first statement whose parameter will match with the exception object will be executed and remaining statements will be skipped.
Let us see a program to understand this concept
class MultipleCatchDemo { public static void main( String args[ ]) { int x[ ]= {1,2,3,4}; int y=2; try { int z=x[4]/y+x[2]; } catch( ArithmeticException e) { System.out.println(“Silan Entertainment”); } catch( ArrayIndexOutOfBoundsException e) { System.out.println(“Silan Software”); } catch( ArrayStoreExceotion e) { System.out.println(“silan technologies”); } System.out.println(x[1]/x[0]); System.out.println(“silantutorial means Java and Java means silantutorial”); } }
Output:
Silan Software
2
silantutorial means Java and Java means silantutorial
At a time only one Exception is occurred and at a time only one catch block is executed.
The try block within a try block is known as nested try block in java. Sometimes a situation may arise where a part of a block may cause one error and the entire block itself may cause another error. In such cases, exception handlers have to be nested.
Syntax .... try { statement 1; statement 2 try { statement 1; statement 2; } catch(Exception e) { } } catch(Exception e) { } ....
Let's see a simple example of java nested try block.
class NestedDemo { public static void main(String args[]) { try { try { int b =5/0; } catch(ArithmeticException e) { System.out.println(e); } try { int a[]=new int[5]; a[5]=15; } catch(ArrayIndexOutOfBoundsException e) { System.out.println(e); } System.out.println("other statement); } catch(Exception e) { System.out.println("handeled"); } System.out.println("normal flow"); } }
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc