Java Programming/Keywords/finally - Wikibooks, Open Books For An ...
Maybe your like
- Book
- Discussion
- Read
- Edit
- Edit source
- View history
- Read
- Edit
- Edit source
- View history
- What links here
- Related changes
- Upload file
- Permanent link
- Page information
- Cite this page
- Get shortened URL
- Download QR code
- Wikipedia
- Wikiversity
- Wiktionary
- Wikiquote
- Wikisource
- Wikinews
- Wikivoyage
- Commons
- Wikidata
- MediaWiki
- Meta-Wiki
- Create a collection
- Download as PDF
- Printable version
finally is a keyword which is an optional ending part of the try block.
| Code section 1: try block. try{ // ... }catch(MyException1e){ // Handle the Exception1 here }catch(MyException2e){ // 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 return statement in the try block.
Three things can happen in a try block. First, no exception is thrown:
|
|
You can see that we have passed in the try block, then we have executed the finally 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"); thrownewMyException1(); System.out.println("Terminate the try block"); }catch(MyException1e){ System.out.println("Handle the Exception1"); }catch(MyException2e){ 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 try block until where the exception occurred, then we have executed the matching catch block, the finally 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"); thrownewException(); System.out.println("Terminate the try block"); }catch(MyException1e){ System.out.println("Handle the Exception1"); }catch(MyException2e){ 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 try block until where the exception occurred and we have executed the finally block. NO CODE after the try-catch block has been executed. If there is an exception that happens before the try-catch block, the finally block is not executed.
If return 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{ return11; }finally{ return12; } |
will return 12, not 11. Professional code almost never contains statements that alter execution order (like return, break, continue) 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
- Book:Java Programming/Keywords
- Pages with deep filing
Tag » What Is Finally Keyword In Java
-
Java Finally Keyword - W3Schools
-
Guide To The Java Finally Keyword - Baeldung
-
Final Keyword In Java - GeeksforGeeks
-
Java Finally Block - Javatpoint
-
Final, Finally And Finalize In Java - Tutorialspoint
-
The Finally Block - Essential Java Classes
-
Java Final Keyword (With Examples) - Programiz
-
Finally Block In Java | Use, Example - Scientech Easy
-
Java Finally Keyword - Finally Java Tutorial - YouTube
-
Java Finally Keyword
-
Java Final Keyword, Variable, Method And Class - TechVidvan
-
Finally Keyword In Java With Examples - TechieClues
-
Try, Catch, Finally And Throw In Java With Examples
-
What Is The Final Keyword In Java?