Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

Java Programming/Keywords/finally

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

is a keyword which is an optional ending part of the block. Code section 1: block. try { // ... } catch (MyException1 e) { // Handle the Exception1 here } catch (MyException2 e) { // Handle the Exception2 here } finally { // This will always be executed no matter what happens } The code inside the finally block will always be executed. This is also true for cases when there is an exception or even executed statement in the try block. Three things can happen in a try block. First, no exception is thrown: Code section 2: No exception is thrown. System.out.println("Before the try block"); try { System.out.println("Inside the try block"); } catch (MyException1 e) { System.out.println("Handle the Exception1"); } catch (MyException2 e) { System.out.println("Handle the Exception2"); } finally { System.out.println("Execute the finally block"); } System.out.println("Continue");Console for Code section 2 Before the try block Inside the try block Execute the finally block Continue You can see that we have passed in the block, then we have executed the block and we have continued the execution. Now, a caught exception is thrown: |Code section 3: A caught exception is thrown. System.out.println("Before the try block"); try { System.out.println("Enter inside the try block"); throw new MyException1(); System.out.println("Terminate the try block"); } catch (MyException1 e) { System.out.println("Handle the Exception1"); } catch (MyException2 e) { System.out.println("Handle the Exception2"); } finally { System.out.println("Execute the finally block"); } System.out.println("Continue"); Console for Code section 3 Before the try block Enter inside the try block Handle the Exception1 Execute the finally block Continue We have passed in the block until where the exception occurred, then we have executed the matching block, the block and we have continued the execution. Now, an uncaught exception is thrown: Code section 4: An uncaught exception is thrown. System.out.println("Before the try block"); try { System.out.println("Enter inside the try block"); throw new Exception(); System.out.println("Terminate the try block"); } catch (MyException1 e) { System.out.println("Handle the Exception1"); } catch (MyException2 e) { System.out.println("Handle the Exception2"); } finally { System.out.println("Execute the finally block"); } System.out.println("Continue"); Console for Code section 4 Before the try block Enter inside the try block Execute the finally block We have passed in the block until where the exception occurred and we have executed the block. NO CODE after the try-catch block has been executed. If there is an exception that happens before the try-catch block, the block is not executed. If statement is used inside finally, it overrides the return statement in the try-catch block. For instance, the construct Code section 5: Return statement. try { return 11; } finally { return 12; } will return 12, not 11. Professional code almost never contains statements that alter execution order (like , , ) inside the finally block, as such code is more difficult to read and maintain. See also: Java Programming/Keywords/try Java Programming/Keywords/catch Java Programming/Throwing and Catching Exceptions#Catching Rule deep