if we catch unchecked exception which would not be raised in try block we will get compile error.
Compilation error : Unreachable catch block for XXXException. This exception is never thrown from the try statement body.
But this rule is applicable for only fully checked exceptions not for partially checked exceptions java.lang.Exception or java.lang.Throwable.
Example 01: catch unchecked exception – which would not be raised in try block
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.vidvaan.corejava.exception20.catcheNotRaised; // catch unchecked exception - which could not be raised in try block public class Example01CatchUncheckedException { public static void main(String[] args) { try { System.out.println("no exception raised"); } catch (IllegalArgumentException e) { e.printStackTrace(); } // allowed - no compilation error } } //correct |
In the above example, syntactically we can catch IllegalArgumentException, But we should not catch IllegalArgumentException. We could avoid this exception with pre-conditions instead with try catch.
Example 02: catch checked exception – which would not be raised in try block
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.vidvaan.corejava.exception20.catcheNotRaised; import java.io.IOException; // catch checked exception - which could not be raised in try block public class Example02CatchCheckedException { public static void main(String[] args) { try { System.out.println("no exception raised"); } catch (IOException e) { e.printStackTrace(); } // Compilation error : Unreachable catch block for IOException. // This exception is never thrown from the try statement body } } //wrong |
In the above example, we catch ‘IOException’ which would not be raised in try block. It leads to compilation error : Unreachable catch block for IOException.
Example 03: catch partially checked exception – which could not be raised in try block
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.vidvaan.corejava.exception20.catcheNotRaised; // catch partially checked exception - which could not be raised in try block public class Example03CatchPartiallyCheckedException { public static void main(String[] args) { try { System.out.println("no exception raised"); } catch (Exception e) { e.printStackTrace(); } // allowed - no compilation error } } // correct |
In the above example, we catch java.lang.Excepiton which would not be raised in try block. It is allowed without error. Here another partially checked exception ‘Throwable’ is also allowed. We can catch both checked and unchecked exceptions with Exception or Throwable classes. In general when we are not sure about what exception would raise, then we have to catch that with either Exception or Throwable.
Example 04: catch Error – which could not be raised in try block
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.vidvaan.corejava.exception20.catcheNotRaised; // catch Error - which could not be raised in try block public class Example04CatchError { public static void main(String[] args) { try { System.out.println("no exception raised"); } catch (AssertionError e) { e.printStackTrace(); } // allowed - no compilation error } } // correct |
In the above example, syntactically we can catch AssertionError, But we should not catch Error and it’s subclasses. Because they are irrecoverable problems.