Nested try catch
Exception handlers can be nested within one another.
It means try, catch or a finally block can in turn contains another set of try catch finally sequence.
In such a scenario, when a particular catch or finally block is unable to handle an Exception, that exception will be rethrown automatically and such exception would be handled by the outer set of try catch handler.
Lets understand this with below use cases and example
Case 1 : Exception handling within try block
In this case, we will write try-catch block within a try block
This way we can modularize the exception handling to only required set of statements
If exception is thrown inside inner try block then it will try to find corresponding catch block within that block, if not found then exception will be thrown back to outer try block and will be handled by outer catch block
Consider below example
- package com.kb.Exception;
- public class ExceptionHandlingTry {
- public static void main(String[] args) {
- try {
- System.out.println("Begin: Outer try block");
- try {
- System.out.println("Begin: Inner try block");
- int arr[] = {1,2,0};
- int res = 10 / arr[2];
- System.out.println(res);
- } catch (ArrayIndexOutOfBoundsException e) {
- System.out.println("ArrayIndexOutOfBoundsException caught");
- } finally {
- System.out.println("Inside Inner final");
- }
- } catch (ArithmeticException e) {
- System.out.println("ArithmeticException caught");
- } finally {
- System.out.println("Inside Outer finally");
- }
- System.out.println("Continue further lines .....");
- }
- }
package com.kb.Exception; public class ExceptionHandlingTry { public static void main(String[] args) { try { System.out.println("Begin: Outer try block"); try { System.out.println("Begin: Inner try block"); int arr[] = {1,2,0}; int res = 10 / arr[2]; System.out.println(res); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException caught"); } finally { System.out.println("Inside Inner final"); } } catch (ArithmeticException e) { System.out.println("ArithmeticException caught"); } finally { System.out.println("Inside Outer finally"); } System.out.println("Continue further lines ....."); } }
We can see that inside inner try block, code is throwing Arithmetic exception but there is no corresponding handler hence its throwing that exception to outer try and hence its handled by outer catch block.
Case 2 : Exception handling within catch block
In this case, we will write try-catch block within a catch block
Sometimes its required to handle the exception within a catch block if we write complex code in catch block.
Consider below example
- package com.kb.Exception;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- public class ExceptionHandlingCatch {
- public static void main(String[] args) {
- try {
- System.out.println("Begin: Outer try block");
- int arr[] = {1,2,0};
- int res = 10 / arr[2];
- System.out.println(res);
- } catch (ArithmeticException e) {
- try {
- System.out.println("Begin: Try within Catch block");
- readExceptionCodeAndPrintDetails(e.getMessage());
- } catch (IOException ex) {
- System.out.println("IOException caught");
- } finally {
- System.out.println("Inside Inner final");
- }
- }
- finally {
- System.out.println("Inside Outer finally");
- }
- System.out.println("Continue further lines .....");
- }
- private static void readExceptionCodeAndPrintDetails(String details) throws IOException {
- System.out.println("Code to read the exception code from File");
- FileReader fileReader =
- new FileReader("exception.txt");
- BufferedReader bufferedReader =
- new BufferedReader(fileReader);
- String line ;
- while(( line = bufferedReader.readLine()) != null) {
- System.out.println(line + "=> "+details);
- }
- bufferedReader.close();
- }
- }
package com.kb.Exception; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class ExceptionHandlingCatch { public static void main(String[] args) { try { System.out.println("Begin: Outer try block"); int arr[] = {1,2,0}; int res = 10 / arr[2]; System.out.println(res); } catch (ArithmeticException e) { try { System.out.println("Begin: Try within Catch block"); readExceptionCodeAndPrintDetails(e.getMessage()); } catch (IOException ex) { System.out.println("IOException caught"); } finally { System.out.println("Inside Inner final"); } } finally { System.out.println("Inside Outer finally"); } System.out.println("Continue further lines ....."); } private static void readExceptionCodeAndPrintDetails(String details) throws IOException { System.out.println("Code to read the exception code from File"); FileReader fileReader = new FileReader("exception.txt"); BufferedReader bufferedReader = new BufferedReader(fileReader); String line ; while(( line = bufferedReader.readLine()) != null) { System.out.println(line + "=> "+details); } bufferedReader.close(); } }
In the above program, we have added try catch exception handling within catch block
Catch block is performing file read operation and hence it may throw an IOException
In case, we are performing some operation within catch block which may throw an exception then its better to write Try-Catch within catch block to handle it
In above program, we are getting Arithmetic exception from outer Try block and its handled by outer Catch block but outer catch block is throwing IOException while reading file as File does not exist.
IOException is then handled by inner catch block.
Case 3 : Exception handling within finally block
In this case, we will write try-catch block within a Finally block
Sometimes its required to handle the exception within a finally block if we write complex code in finally block.
Consider below example
- package com.kb.Exception;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- public class ExceptionHandlingFinally {
- public static void main(String[] args) {
- FileReader fileReader = null;
- BufferedReader bufferedReader = null;
- try {
- System.out.println("Code to read the data from File");
- fileReader = new FileReader("sample.txt");
- bufferedReader = new BufferedReader(fileReader);
- String line;
- while ((line = bufferedReader.readLine()) != null) {
- System.out.println(line);
- }
- } catch (IOException e) {
- System.out.println("Inside Outer catch block");
- } finally {
- try {
- System.out.println("Begin: Finally block");
- bufferedReader.close();
- fileReader.close();
- } catch (Exception ex) {
- System.out.println("Exception caught while closing file");
- }
- }
- System.out.println("Continue further lines .....");
- }
- }
package com.kb.Exception; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class ExceptionHandlingFinally { public static void main(String[] args) { FileReader fileReader = null; BufferedReader bufferedReader = null; try { System.out.println("Code to read the data from File"); fileReader = new FileReader("sample.txt"); bufferedReader = new BufferedReader(fileReader); String line; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { System.out.println("Inside Outer catch block"); } finally { try { System.out.println("Begin: Finally block"); bufferedReader.close(); fileReader.close(); } catch (Exception ex) { System.out.println("Exception caught while closing file"); } } System.out.println("Continue further lines ....."); } }
In the above program,We have written exception handling code in finally block as well
In try block, we are trying to read the content of a file
In finally, we are trying to close the file but bufferedReader points to null , it will throw an exception while closing a file and that is caught in the catch block within the finally block. So its required to handle exceptions within finally as well.
Note :
Its not good to use nested try-catch unless there is a necessity of doing so.