Java Programs On Exception Handling for Interview

The finally block always executes when the try block exits.

 This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. 

Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated

It is never recommended to take clean-up code inside try block because there is no guarantee for the execution of every statement inside a try. It is never recommended to place clean-up code inside catch block because if there is no exception then catch block won't be executed. 

 We require someplace to maintain clean up code which should be executed always irrespective of whether exception raised or not raised and whether handled or not handled such type of place is nothing but finally block. Hence the main objective of finally block is to maintain cleanup code.

Example: Try { risky code } catch(x e) { handling code } finally { cleanup code }

The specialty of the finally block is it will be executed always irrespective of whether the exception is raised or not raised and whether handled or not handled.
public class Finally1 { public static void main(String[] args) { try { System.out.println("try block is executed"); } catch(ArithmeticException e) { System.out.println("catch block is executed"); } finally { System.out.println("finally block is executed"); } } } output: try block is executed finally block is executed

public class Finally2 { public static void main(String[] args) { try { System.out.println("try block is executed."); System.out.println(10 / 0); } catch(ArithmeticException e) { System.out.println("catch block is executed."); } finally { System.out.println("finally block is executed."); } } } output: try block is executed. catch block is executed. finally block is executed.

public class Finally3 { public static void main(String[] args) { try { System.out.println("try block is executed."); System.out.println(10 / 0); } catch(NullPointerException e) { System.out.println("catch block is executed."); } finally { System.out.println("finally block is executed."); } } } output: try block is executed. finally block is executed. Exception in thread "main" java.lang.ArithmeticException: / by zero at Main.main(Main.java:15)

Finally, block dominates return statement

Even though return present in try or catch blocks first finally will be executed and after that only return statement will be considered that finally, blocks dominates return statement.

public class Finally4 { public static void main(String[] args) { try { System.out.println("try block is executed."); return; } catch(ArithmeticException e) { System.out.println("catch block is executed."); } finally { System.out.println("finally block is executed."); } } } output: try block is executed. finally block is executed.

If the return statement presents try-catch and finally blocks then finally block return statement will be considered.

public class Main { public static void main(String[] args) { System.out.println(methodOne()); } public static int methodOne() { try { System.out.println(10 / 0); return 1; } catch(ArithmeticException e) { return 2; } finally { return 3; } } } output: 3

System.exit(0); dominates finally block

There is only one situation where the finally block won't be executed is whenever we are using System.exit(0) method. Then JVM itself will be shut down, in this case finally block won't be executed. i.e., System.exit(0); dominates finally block

public class Main { public static void main(String[] args) { try { System.out.println("try block is executed."); System.exit(0); } catch(ArithmeticException e) { System.out.println("catch block is executed."); } finally { System.out.println("finally block is executed."); } } } output: try block is executed.

Whenever we are writing try block compulsory we should write either catch or finally. i.e., try without catch or finally is invalid.

public class Example1 { public static void main(String[] args) { try {} catch(ArithmeticException e) {} System.out.println("Compile and Run Successfully."); } } output: Compile and Run Successfully.

public class Example2 { public static void main(String[] args) { try {} catch(ArithmeticException e) {} catch(NullPointerException e) {} System.out.println("Compile and Run Successfully."); } } output: Compile and Run Successfully.

When we redefine catch block, an exception will be thrown - Exception has already been caught

public class Example3 { public static void main(String[] args) { try {} catch(ArithmeticException e) {} catch(NullPointerException e) {} System.out.println("Compile and Run Successfully."); } } output: Example3.java:17: error: exception ArithmeticException has already been caught catch(ArithmeticException e) ^ 1 error

When we use try without a catch and finally, we will get errors.

public class Example4 { public static void main(String[] args) { try {} } } } output: Example4.java:11: error: 'try' without 'catch', 'finally' or resource declarations try {} ^ Example4.java:14: error: class, interface, or enum expected

when we define catch' block without 'try' block Exception will be thrown - catch' without 'try'

public class Example5 { public static void main(String[] args) { catch(Exception e) {} } } } output: Example5.java:11: error: 'catch' without 'try' catch(Exception e) ^ Example5.java:16: error: class, interface, or enum expected } ^ 2 errors

If we put the statement in between try and catch block, Exception will be thrown - 'try' without 'catch', 'finally' or resource declarations

public class Example6 { public static void main(String[] args) { try {} System.out.println("in between ..."); catch(Exception e) {} } } output: Example6.java:10: error: 'try' without 'catch', 'finally' or resource declarations try ^ Example6.java:13: error: 'catch' without 'try' catch(Exception e) ^ 2 errors

if we put try-catch and finally block without any statement - no error will be thrown.

public class Example7 { public static void main(String[] args) { try {} catch(Exception e) {} finally {} System.out.println("Compile and Run Successfully."); } } output: Compile and Run Successfully.

If we use try block without a catch but with finally block, no error will be thrown.

public class Example8 { public static void main(String[] args) { try {} finally {} System.out.println("Compile and Run Successfully."); } } output: Compile and Run Successfully.

If we use try without a catch but with finally blocks, the error will be thrown - 'finally' without 'try'

public class Example9 { public static void main(String[] args) { try {} finally {} finally {} } } output: Example9.java:14: error: 'finally' without 'try' finally ^ 1 error

If we put try-catch, but a statement in between catch and finally blocks, error will be thrown - 'finally' without 'try'

public class Example10 { public static void main(String[] args) { try {} catch(Exception e) {} System.out.println("in between .."); finally {} } } output: Example10.java:15: error: 'finally' without 'try' finally ^ 1 error

If we put catch block after finally block, an error will be thrown - 'catch' without 'try'

public class Example11 { public static void main(String[] args) { try {} finally {} catch(Exception e) {} } } output: Example11.java:14: error: 'catch' without 'try' catch(Exception e) ^ 1 error

If we put finally block without try-catch , an error will be thrown - 'finally' without 'try'

public class Example12 { public static void main(String[] args) { finally {} } } output: Example12.java:10: error: 'finally' without 'try' finally ^ 1 error

If we put nested try-catch - no error will be thrown, as nesting of try-catch is possible.

public class Example13 { public static void main(String[] args) { try { try {} catch(Exception e) {} } catch(Exception e) {} System.out.println("Compile and Run Successfully."); } } output: Compile and Run Successfully.

Post/Questions related to Java Programs On Exception Handling


Java try-catch Examples
Try with Resources statement in Java
Checked and Unchecked Exceptions in Java

In this article, we have seen Java Programs On Exception Handling for Interview.