Here is a Java Program to Demonstrate the Finally Block
Output of Above Java Program
inside procA
procA's finally
Exception caught
inside procB
procB's finally
inside procC
procC's finally
class FinallyDemo { // Through an exception out of the method. static void procA() { try { System.out.println("inside procA"); throw new RuntimeException("demo"); } finally { System.out.println("procA's finally"); } } // Return from within a try block. static void procB() { try { System.out.println("inside procB"); return; } finally { System.out.println("procB's finally"); } } // Execute a try block normally. static void procC() { try { System.out.println("inside procC"); } finally { System.out.println("procC's finally"); } } public static void main(String args[]) { try { procA(); } catch (Exception e) { System.out.println("Exception caught"); } procB(); procC(); } }
Output of Above Java Program
inside procA
procA's finally
Exception caught
inside procB
procB's finally
inside procC
procC's finally