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
Java syntax
(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!
===Exception handling statements=== ====<code>try-catch-finally</code> statements==== Exceptions are managed within <code>try</code> ... <code>catch</code> blocks. <syntaxhighlight lang="java"> try { // Statements that may throw exceptions methodThrowingExceptions(); } catch (Exception ex) { // Exception caught and handled here reportException(ex); } finally { // Statements always executed after the try/catch blocks freeResources(); } </syntaxhighlight> The statements within the <code>try</code> block are executed, and if any of them throws an exception, execution of the block is discontinued and the exception is handled by the <code>catch</code> block. There may be multiple <code>catch</code> blocks, in which case the first block with an exception variable whose type matches the type of the thrown exception is executed. Java SE 7 also introduced multi-catch clauses besides uni-catch clauses. This type of catch clauses allows Java to handle different types of exceptions in a single block provided they are not subclasses of each other. <syntaxhighlight lang="java"> try { methodThrowingExceptions(); } catch (IOException | IllegalArgumentException ex) { //Both IOException and IllegalArgumentException will be caught and handled here reportException(ex); } </syntaxhighlight> If no <code>catch</code> block matches the type of the thrown exception, the execution of the outer block (or method) containing the <code>try</code> ... <code>catch</code> statement is discontinued, and the exception is passed up and outside the containing block (or method). The exception is propagated upwards through the [[call stack]] until a matching <code>catch</code> block is found within one of the currently active methods. If the exception propagates all the way up to the top-most <code>main</code> method without a matching <code>catch</code> block being found, a textual description of the exception is written to the standard output stream. The statements within the <code>finally</code> block are always executed after the <code>try</code> and <code>catch</code> blocks, whether or not an exception was thrown and even if a <code>return</code> statement was reached. Such blocks are useful for providing cleanup code that is guaranteed to always be executed. The <code>catch</code> and <code>finally</code> blocks are optional, but at least one or the other must be present following the <code>try</code> block. ====<code>try</code>-with-resources statements==== <code>try</code>-with-resources statements are a special type of <code>try-catch-finally</code> statements introduced as an implementation of the [[dispose pattern]] in Java SE 7. In a <code>try</code>-with-resources statement the <code>try</code> keyword is followed by initialization of one or more resources that are released automatically when the <code>try</code> block execution is finished. Resources must implement <code>java.lang.AutoCloseable</code>. <code>try</code>-with-resources statements are not required to have a <code>catch</code> or <code>finally</code> block unlike normal <code>try-catch-finally</code> statements. <syntaxhighlight lang="java"> try (FileOutputStream fos = new FileOutputStream("filename"); XMLEncoder xEnc = new XMLEncoder(fos)) { xEnc.writeObject(object); } catch (IOException ex) { Logger.getLogger(Serializer.class.getName()).log(Level.SEVERE, null, ex); } </syntaxhighlight> Since Java 9 it is possible to use already declared variables: <syntaxhighlight lang="java"> FileOutputStream fos = new FileOutputStream("filename"); XMLEncoder xEnc = new XMLEncoder(fos); try (fos; xEnc) { xEnc.writeObject(object); } catch (IOException ex) { Logger.getLogger(Serializer.class.getName()).log(Level.SEVERE, null, ex); } </syntaxhighlight> ====<code>throw</code> statement==== The <code>throw</code> statement is used to throw an exception and end the execution of the block or method. The thrown exception instance is written after the <code>throw</code> statement. <syntaxhighlight lang="java"> void methodThrowingExceptions(Object obj) { if (obj == null) { // Throws exception of NullPointerException type throw new NullPointerException(); } // Will not be called, if object is null doSomethingWithObject(obj); } </syntaxhighlight>
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)