The exception we caught in catch block will be throw again from catch block is called rethrow exception. We can rethrow exception two ways.
- Re-throw an exception we caught
- Re-throw wrapped exception
The purpose of the rethrow operation is to propagate the exception to the higher level, But before propagation we would perform some activities like logging, send an email, audit error, rollback transaction … etc. We can perform such activities in the catch block and re-throw the exception again. In this way, a higher level gets notified that the exception has occurred in the application.
Example 01 : Re-throw an exception we caught
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.vidvaan.corejava.exception24.rethrow; //re-throw an exception we caught public class Example01ReThrowCaughtException { public static void main(String[] args) { System.out.println("main begin"); try { processName(); } catch (Exception e) { System.out.println("Unable to Process Name !!!"); } System.out.println("main end"); } public static void processName() { String firstName = "Sekhar"; String lastName = null; try { fullName(firstName, lastName); } catch (Exception e) { // Here we are logging exception before rethrow // logger.error("unable to evaluate fullname", e); throw e; // rethrowing same exception we caught } } public static String fullName(String firstName, String lastName) { System.out.println("processing fullName ..."); return lastName.concat(firstName); } } |
Output :
main begin
processing fullName …
Unable to Process Name !!!
main end
In the above example, we are rethrowing exception we caught, before rethrowing we are logging the error. But we can also do auditing error or send email or what ever we want before rethrowing.
Example 02 : Re-throw wrapped exception
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package com.vidvaan.corejava.exception24.rethrow; //re-throw wrapped exception public class Example02ReThrowWrappedException { public static void main(String[] args) { System.out.println("main begin"); try { processName(); } catch (Exception e) { System.out.println("Unable to Process Name !!!"); } System.out.println("main end"); } public static void processName() { String firstName = "Sekhar"; String lastName = null; try { fullName(firstName, lastName); } catch (Exception e) { // Here we are logging exception before rethrow // logger.error("unable to evaluate fullname", e); System.out.println("unable to evaluate fullname"); throw new IllegalArgumentException("Unable to evaluate fullname", e); // rethrowing by wrapping/encapsulating caught exception into IllegalArgumentException } } public static String fullName(String firstName, String lastName) { System.out.println("processing fullName ..."); return lastName.concat(firstName); } } |
Output :
main begin
processing fullName …
unable to evaluate fullname
Unable to Process Name !!!
main end
In the above example, we caught exception and encapsulate that into IllegalArgumentException and then rethrown. Here we are encapsulating caught exception into pre-defined exception. But we can also encapsulate into user defined exception.