Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A <code>RuntimeException</code> should be used only when the client cannot recover from whatever the problem is. It is occasionally appropriate to do what you are talking about, but more often it is not appropriate.</p> <p>If you are using a JDK >= 1.4, then you can do something like:</p> <pre> try { // Code that might throw an exception } catch (IOException e) { throw new RuntimeException(e); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } </pre> <p>and the rethrown <code>RuntimeException</code> will have the original cause included inside it. This way, someone at the top of the thread catching the <code>RuntimeException</code> -- your threads do catch <code>RuntimeException</code> so they don't just silently die, right? -- can at least print out the FULL stack trace of the cause.</p> <p>But as others have said and will say, exceptions are checked for a reason. Only do this when you are positive that your clients cannot recover from the problem that you are rethrowing as an unchecked exception.</p> <p>NOTE: Better than just <code>RuntimeException</code> would be to use a more specific unchecked exception if one is available. For example, if the only reason your method could throw a <code>ClassNotFoundException</code> is because a configuration file is missing, you could rethrow a <code>MissingResourceException</code>, which is an unchecked exception but gives more information about why you are throwing it. Other good <code>RuntimeException</code>s to use if they describe the problem you are rethrowing are <code>IllegalStateException</code>, <code>TypeNotPresentException</code> and <code>UnsupportedOperationException</code>.</p> <p>Also note that it is ALWAYS a good idea for your threads to catch RuntimeException and at a minimum log it. At least this way you understand why your threads are going away.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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