Flow Chart of Java finally block
public class Main { public static void main(String args[]){ try{ // try block int arr[]=new int[10]; arr[15]=15/0; System.out.println(arr[15]); } catch(ArithmeticException e){ System.out.println(e); } catch(ArrayIndexOutOfBoundsException e){ System.out.println(e); } finally { System.out.println("The finally block is always executed."); } System.out.println("This Code after finally block"); } }
Output:
java.lang.ArithmeticException: / by zero
The finally block is always executed.
This Code after finally block
package DockerTpoint; public class Main { public static void main(String args[]){ try{ // try block System.out.println("Inside try block"); int Num=15/0; System.out.println(Num); } catch(ArrayIndexOutOfBoundsException e){ //it cannot handle Arithmetic type exception System.out.println(e); } finally { System.out.println("The finally block is always executed."); } System.out.println("This Code after " + " finally block"); } }
Output:
Inside try block
The finally block is always executed.
Exception in thread "main" java.lang.ArithmeticException: / by zero
at HELLO/DockerTpoint.Main.main(Main.java:7)
The try block in the example below has two exceptions. However, only one exception occurs at a time, and its associated catch block is executed.
package DockerTpoint; public class Main { public static void main(String args[]){ try{ // try block System.out.println("Inside try block"); int Num=15/3; System.out.println(Num); } catch(ArrayIndexOutOfBoundsException e){ //it cannot handle Arithmetic type exception System.out.println(e); } finally { System.out.println("The finally block is always executed."); } System.out.println("This Code after " + " finally block"); } }
Output:
Inside try block
5
The finally block is always executed.
This Code after finally block
Post your comment