Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, don't catch <code>Exception</code>. Catch a specific subclass.</p> <p>Second, yes it may be that an underlying <code>IOException</code>, which is what this looks like, should be wrapped in an app-specific exception. You could have, for instance:</p> <pre><code>final class DBGlobalsException extends Exception { Field somethingDBGlobalsSpecific; //where this is shorthand for chained constructors that look like Exception DBGlobalsException(String message, Throwable cause) { super(message,cause); } // elaborate your exception with whatever error state you want to propagate outwards void setField(Field ...) {} } </code></pre> <p>And you could then </p> <pre><code>catch (IOException ex) { DBGlobals.Error("OHMjson.Graph.saveLastGraphName - Error: " + ex.getMessage()); msg = "Unable to save data"; status = false; DBGlobalsException dbe = new DBGlobalsException(msg,ex); dbe.setField(status /* or whatever */); throw dbe; } </code></pre> <p>See also: <a href="https://stackoverflow.com/questions/986695/in-java-is-using-throws-exception-instead-of-throwing-mulitple-specific-exceptio">Throwing <code>Exception</code></a>, <a href="https://stackoverflow.com/questions/2716749/throwing-a-new-exception-while-throwing-an-old-exception">Throwing new and old exceptions</a>, <a href="https://stackoverflow.com/questions/237585/java-lang-exception-vs-rolling-your-own-exception">Rolling your own</a>. That's just a few, there are a bunch of great SO Q+A's on exception handling, chaining, etc. Some useful best-practices stuff outlined at <a href="http://en.wikibooks.org/wiki/Java_Programming/Throwing_and_Catching_Exceptions" rel="nofollow noreferrer">WikiBooks</a>.</p> <p>Also read <a href="https://stackoverflow.com/questions/3950629/expection-handling/3950652#3950652">Oded's answer</a> and think it through... your exception should encapsulate whatever status (error codes, flags, etc...) are required for the outer code to recover from the condition -- or it should be an unrecoverable condition (see <code>RuntimeException</code>).</p> <p>Finally, the canonical Effective Java reference: <a href="http://books.google.com/books?id=ZZOiqZQIbRMC&amp;lpg=PA178&amp;ots=UZS24scK36&amp;dq=throw%20exceptions%20%22effective%20java%22&amp;pg=PA178#v=onepage&amp;q&amp;f=false" rel="nofollow noreferrer">Item 43: Throw Exceptions Appropriate to the Abstraction</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload