Catch an Error and Then Continue the Program
In this guide, you will learn how to use try-catch along with finally block in Java. We will cover various examples to see, how try catch and finally works together during exception handling.
Scenario 1: Exception doesn't occur in try block
If exception doesn't occur in try block then all the catch blocks are ignored, however the finally block executes (if it is present) because it executes whether exception occurs or not.
Case 1: try-catch block with no finally block:
class JavaExample { public static void main(String args[]) { int x = 10; int y = 10; try{ int num= x/y; System.out.println("Remaining statements inside try block"); } catch(Exception ex) { System.out.println("Exception caught in catch block"); } System.out.println("Statements Outside of try-catch"); } }
Output:
Remaining statements inside try block Statements Outside of try-catch
Case 2: Same scenario with try-catch-finally clause:
public class JavaExample { public static void main(String args[]){ //declared and initialized two variables int num1 =10, num2 = 5; try { int div = num1/num2; // if exception occurs in the above statement then this // statement will not execute else it will execute System.out.println("num1/num2: "+div); } catch(ArithmeticException e) { System.out.println("Catch block: ArithmeticException caught"); } finally{ System.out.println("Finally block: I will always execute"); } // rest of the code System.out.println("Outside try-catch-finally"); } }
Output:
Scenario 2: Exception occurred in try block and handled in catch block
If exception occurs in try block then the rest of the statements inside try block are ignored and the corresponding catch block executes. After catch block, the finally block executes and then the rest of the program.
In the following example, an Arithmetic exception occurred as the number is divided by zero, there is a catch block to handle Arithmetic exception so the control got transferred to it. After which the statements inside finally block (if present) are executed.
Case 1: try-catch without finally:
class JavaExample { public static void main(String args[]) { int x = 10; int y = 0; try{ int num= x/y; System.out.println("Remaining statements inside try block"); } catch(Exception ex) { System.out.println("Exception caught in catch block"); } System.out.println("Statements Outside of try-catch"); } }
Output:
Exception caught in catch block Statements Outside of try-catch
Point to note in above example: There are two statements present inside try block. Since exception occurred because of first statement, the second statement didn't execute. Hence we can say that if an exception occurs then the rest of the statements in try block don't execute and control passes to catch block.
Case 2: try-catch with finally:
public class JavaExample { public static void main(String args[]){ //now the second variable is initialized with 0 value int num1 =10, num2 = 0; try { int div = num1/num2; // if exception occurs in the above statement then this // statement will not execute else it will execute System.out.println("num1/num2: "+div); } catch(ArithmeticException e) { System.out.println("Catch block: ArithmeticException caught"); } finally{ System.out.println("Finally block: I will always execute"); } // rest of the code System.out.println("Outside try-catch-finally"); } }
Output:
Scenario 3: Exception occurred in try block and not handled in catch block
If the exception raised in try block is not handled in catch block then the rest of the statements in try block and the statements after try-catch-finally doesn't execute, only the finally block executes and a system generated error message for the exception that occurred in try block.
In the following example, the ArithmeticException occurred in try block but there is no catch block that can handle ArithmeticException so after the execution of finally block, a system generated error message is displayed.
public class JavaExample { public static void main(String args[]){ //now the second variable is initialized with 0 value int num1 =10, num2 = 0; try { int div = num1/num2; // if exception occurs in the above statement then this // statement will not execute else it will execute System.out.println("num1/num2: "+div); } //this cannot handle ArithmeticException catch(ArrayIndexOutOfBoundsException e) { System.out.println("Catch block: ArrayIndexOutOfBoundsException caught"); } finally{ System.out.println("Finally block executed"); } // rest of the code System.out.println("Outside try-catch-finally"); } }
Output:
Flow of control in try catch finally in Java:
To summarise everything we have learned so far:
- If exception occurs in try block then control immediately transfers(skipping rest of the statements in try block) to the catch block. Once catch block finished execution then finally block and after that rest of the program.
- If no exception occurs in try block, then try block gets executed completely and then control gets transferred to finally block (skipping catch blocks), after which rest of the statements after try-catch-finally are executed.
Recommended Posts:
- Try catch in Java
- Finally in Java
- Throw vs throws in Java
- Multiple catch blocks in Java
Source: https://beginnersbook.com/2013/05/flow-in-try-catch-finally/
0 Response to "Catch an Error and Then Continue the Program"
Enregistrer un commentaire