Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Comparison of C Sharp and Java
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== Finally blocks ==== Java allows flow of control to leave the {{mono|finally}} block of a {{mono|try}} statement, regardless of the way it was entered. This can cause another control flow statement (such as {{mono|return}}) to be terminated mid-execution. For example: <syntaxhighlight lang="java"> int foo() { try { return 0; } finally { return 1; } } </syntaxhighlight> In the above code, the {{mono|return}} statement within the {{mono|try}} block causes control to leave it, and thus {{mono|finally}} block is executed before the actual return happens. However, the {{mono|finally}} block itself also performs a return. Thus, the original return that caused it to be entered is not executed, and the above method returns 1 rather than 0. Informally speaking, it ''tries'' to return 0 but ''finally'' returns 1. C# does not allow any statements that allow control flow to leave the {{mono|finally}} block prematurely, except for {{mono|throw}}. In particular, {{mono|return}} is not allowed at all, {{mono|goto}} is not allowed if the target label is outside the {{mono|finally}} block, and {{mono|continue}} and {{mono|break}} are not allowed if the nearest enclosing loop is outside the {{mono|finally}} block.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)