We can’t convert checked to unchecked or unchecked to checked exception. But we can encapsulate checked exception into unchecked exception and vise versa . By catching checked exception and encapsulating into unchecked exception we will get the feel of converting checked to unchecked. But really we are not converting exception here, we are just encapsulating checked into unchecked.
In spring framework in most of the modules, they will catch checked exception and they will encapsulate checked exception into unchecked exception and then they will throw unchecked exceptions to the callers.
Example 01 : Encapsulate checked exception into unchecked 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
package com.vidvaan.corejava.exception25.convert; // Encapsulate Checked Into UncheckedException public class Example01EncapsulateCheckedIntoUncheckedException { public static void main(String[] args) { System.out.println("main begin"); try { encapsulateCheckedIntoUnchecked(); } catch (UnCheckedException e) { System.out.println("catched UnCheckedException"); e.printStackTrace(); } System.out.println("main end"); } public static void encapsulateCheckedIntoUnchecked() { try { checkedExceptionMethod(); } catch (CheckedException e) { System.out.println("catched CheckedException, convert into UnCheckedException"); // Here CheckedException encapsulating into CheckedException throw new UnCheckedException("unchecked exception", e); } } public static void checkedExceptionMethod() throws CheckedException { System.out.println("checked exception triggering "); throw new CheckedException("checked exception"); } } class CheckedException extends Exception { private static final long serialVersionUID = 1L; public CheckedException() { super(); } public CheckedException(String message, Throwable cause) { super(message, cause); } public CheckedException(String message) { super(message); } public CheckedException(Throwable cause) { super(cause); } } class UnCheckedException extends RuntimeException { private static final long serialVersionUID = 1L; public UnCheckedException() { super(); } public UnCheckedException(String message, Throwable cause) { super(message, cause); } public UnCheckedException(String message) { super(message); } public UnCheckedException(Throwable cause) { super(cause); } } |
Output :
main begin
checked exception triggering
catched CheckedException, convert into UnCheckedException
catched UnCheckedException
com.vidvaan.corejava.exception25.convert.UnCheckedException: unchecked exception
at com.vidvaan.corejava.exception25.convert.Example01EncapsulateCheckedIntoUncheckedException.encapsulateCheckedIntoUnchecked(Example01EncapsulateCheckedIntoUncheckedException.java:25)
at com.vidvaan.corejava.exception25.convert.Example01EncapsulateCheckedIntoUncheckedException.main(Example01EncapsulateCheckedIntoUncheckedException.java:10)
Caused by: com.vidvaan.corejava.exception25.convert.CheckedException: checked exception
at com.vidvaan.corejava.exception25.convert.Example01EncapsulateCheckedIntoUncheckedException.checkedExceptionMethod(Example01EncapsulateCheckedIntoUncheckedException.java:32)
at com.vidvaan.corejava.exception25.convert.Example01EncapsulateCheckedIntoUncheckedException.encapsulateCheckedIntoUnchecked(Example01EncapsulateCheckedIntoUncheckedException.java:21)
… 1 more
main end
In the above example, We are catching checked exception and encapsulate into unchecked. Even we can do vice versa as shown in the following example.
Example 02 : Encapsulate unchecked exception into checked 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 |
package com.vidvaan.corejava.exception25.convert; // Encapsulate Unchecked Into checked Exception public class Example02EncapsulateUncheckedIntoCheckedException { public static void main(String[] args) { System.out.println("main begin"); try { encapsulateCheckedIntoUnChecked(); } catch (CheckedException e) { System.out.println("catched CheckedException"); e.printStackTrace(); } System.out.println("main end"); } public static void encapsulateCheckedIntoUnChecked() throws CheckedException { try { uncheckedExceptionMethod(); } catch (UnCheckedException e) { System.out.println("catched UnCheckedException, convert into CheckedException"); // Here UnCheckedException encapsulating into UnCheckedException throw new CheckedException("checked exception", e); } } public static void uncheckedExceptionMethod() throws UnCheckedException { System.out.println("unchecked exception triggering "); throw new UnCheckedException("unchecked exception"); } } |
Output :
main begin
unchecked exception triggering
catched UnCheckedException, convert into CheckedException
catched CheckedException
com.vidvaan.corejava.exception25.convert.CheckedException: checked exception
at com.vidvaan.corejava.exception25.convert.Example02EncapsulateUncheckedIntoCheckedException.encapsulateCheckedIntoUnChecked(Example02EncapsulateUncheckedIntoCheckedException.java:25)
at com.vidvaan.corejava.exception25.convert.Example02EncapsulateUncheckedIntoCheckedException.main(Example02EncapsulateUncheckedIntoCheckedException.java:10)
Caused by: com.vidvaan.corejava.exception25.convert.UnCheckedException: unchecked exception
at com.vidvaan.corejava.exception25.convert.Example02EncapsulateUncheckedIntoCheckedException.uncheckedExceptionMethod(Example02EncapsulateUncheckedIntoCheckedException.java:32)
at com.vidvaan.corejava.exception25.convert.Example02EncapsulateUncheckedIntoCheckedException.encapsulateCheckedIntoUnChecked(Example02EncapsulateUncheckedIntoCheckedException.java:21)
… 1 more
main end