Demonstrating the finally Block

Figure 11.5 demonstrates that the finally block executes even if an exception is not thrown in the corresponding try block. The program contains static methods main (lines 6–18), throwException (lines 21–44) and doesNotThrowException (lines 47–64). Methods throwException and doesNotThrowException are declared static, so main can call them directly without instantiating a UsingExceptions object.

 1   // Fig. 11.5: UsingExceptions.java 2   // try...catch...finally exception handling mechanism. 3  4   public class UsingExceptions 5   { 6      public static void main(String[] args) 7      { 8         try 9         {10            throwException();11         }12         catch (Exception ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.