try{ //code that could raise an exception }
catch{ // statement(s) that handle an exception }
public class Main { public static void main(String args[]){ int num=20/0; //it will arise exception System.out.println("Hello World"); } }
Output:
java.lang.ArithmeticException: / by zero
This Code is unaffected to exception handling
Exception is arise when we try to divide by zero so rest of code(Hello World) not printed to handle this situation we use exception handling
package DockerTpoint; public class Main { public static void main(String args[]){ try{ int Num=10/0; //code inside try block may raise exception System.out.println(Num); } catch(ArithmeticException e){ e.printStackTrace(); } // code below catch block will execute normally System.out.println("This Code is unaffected to exception handling"); } }
Output:
java.lang.ArithmeticException: / by zero
at HELLO/DockerTpoint.Main.main(Main.java:5)
This Code is unaffected to exception handling
Exception is arise and handled by catch block and rest of code(This Code is unaffected to exception handling) get printed this is reason why we use exception handling
package DockerTpoint; public class Main { public static void main(String args[]){ try{ int Num=10/0; //code inside try block may raise exception System.out.println(Num); System.out.println("Code Inside Try Block"); } catch(ArithmeticException e){ e.printStackTrace(); } // code below catch block will execute normally System.out.println("This Code after catch block"); } }
Output:
java.lang.ArithmeticException: / by zero
at HELLO/DockerTpoint.Main.main(Main.java:6)
This Code after catch block
Exception is arise and handled by catch block but code inside try block is not get printed but rest of code(This Code after catch block) get printed
package DockerTpoint; public class Main { public static void main(String args[]){ try{ int Num=10/0; //code inside try block may raise exception System.out.println(Num); System.out.println("Code Inside Try Block"); } catch(ArithmeticException e){ System.out.println("unable to divide by zero"); } System.out.println("This Code after catch block"); } }
Output:
unable to divide by zero
This Code after catch block
Exception is arise and handled by catch block but catch block print custom message
package DockerTpoint; public class Main { public static void main(String args[]){ int Num1=10; int Num2=0; try{ System.out.println(Num1/Num1/Num2); System.out.println("Code Inside Try Block"); } catch(ArithmeticException e){ Num2=5; System.out.println(Num1/Num2); } System.out.println("This Code after catch block"); } }
Output:
2
This Code after catch block
Exception is arise and handled but resolvd the exception in catch block
Java allows the use of try blocks inside of other try blocks. The term "nested try block" describes it. The context of the exception is pushed onto the stack with each statement we enter in a try block.
For example in the case, the outer try block can handle the ArrayIndexOutOfBoundsException while the inner try block can handle the ArithemeticException.
There are times when a block may contain a portion that causes one error while the entire block itself causes a different error. Such circumstances necessitate nesting of exception handlers.
public class Main { public static void main(String args[]){ try{ try{ //inner try block 1 System.out.println("inner try block"); int Num1 =10/0; } catch(ArithmeticException e){ //inner catch block 1 System.out.println(e); } try{ //inner try block 2 int arr[]=new int[10]; arr[15]=15; } catch(ArrayIndexOutOfBoundsException e){ //inner catch block 2 System.out.println(e); } } catch(Exception e){ e.printStackTrace(); } System.out.println("This Code after outer catch block"); } }
Output:
inner try block
java.lang.ArithmeticException: / by zero
java.lang.ArrayIndexOutOfBoundsException: Index 15 out of bounds for length 10
This Code after outer catch block
Post your comment