Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two types of exceptions, <em>checked</em> and <em>unchecked</em>. Any <code>Throwable</code> is either one or the other.</p> <p>An example of a <em>checked</em> exception is <code>IOException</code>; probably the most (in)famous <em>unchecked</em> exception is <code>NullPointerException</code>.</p> <p>Any <em>checked</em> exceptions that a method may <code>throw</code> must be declared in its <code>throws</code> clause. When you <code>@Override</code> a method (either implementing an <code>interface</code> method or overriding an inherited method from a superclass), certain requirements must be met, and one of them is that the <code>throws</code> clause must not cause a conflict. Simplistically speaking, subclasses/implementations can throw <em>LESS</em>, not <em>MORE</em> checked exceptions.</p> <p>An <em>unchecked exception</em> is defined as <a href="http://download.oracle.com/javase/6/docs/api/java/lang/RuntimeException.html" rel="nofollow noreferrer"><code>RuntimeException</code></a> and its subclasses, and <a href="http://download.oracle.com/javase/6/docs/api/java/lang/Error.html" rel="nofollow noreferrer"><code>Error</code></a> and its subclasses. They do not have to be declared in a method's <code>throws</code> clause.</p> <p>So in this particular case, if you want to <code>throw</code> a <code>CustomException</code> in an implementation of an <code>interface</code> method that does not list it in its <code>throws</code> clause, you can make <code>CustomException extends RuntimeException</code>, making it <em>unchecked</em>. (It can also <code>extends</code> any subclass of <code>RuntimeException</code>, e.g. <code>IllegalArgumentException</code> or <code>IndexOutOfBoundsException</code> may be more appropriate in some cases).</p> <p>This will allow you to compile the code as you desire, but note that the choice between choosing checked vs unchecked exception should not be taken too lightly. This is a contentious issue for many, and there are many factors to consider other than just getting the code to compile the way you want it. You may want to consider a redesign of the <code>interface</code> rather than having implementors throwing various undocumented unchecked exceptions not specified by the <code>interface</code> contract.</p> <h3>References</h3> <ul> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/exceptions.html#11.2" rel="nofollow noreferrer">JLS 11.2 Compile-Time Checking of Exceptions</a></li> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.6" rel="nofollow noreferrer">JLS 8.4.6 Method Throws</a> <blockquote> <p>A method that overrides or hides another method, including methods that implement <code>abstract</code> methods defined in interfaces, may not be declared to <code>throw</code> more checked exceptions than the overridden or hidden method.</p> </blockquote></li> </ul> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/499437/in-java-when-should-i-create-a-checked-exception-and-when-should-it-be-a-runtim">In Java, when should I create a checked exception, and when should it be a runtime exception?</a></li> <li><a href="https://stackoverflow.com/questions/27578/when-to-choose-checked-and-unchecked-exceptions"> When to choose checked and unchecked exceptions </a></li> <li><a href="https://stackoverflow.com/questions/613954/the-case-against-checked-exceptions">The case against checked exceptions</a></li> </ul> <h3>See also</h3> <ul> <li><em>Effective Java 2nd Edition</em> <ul> <li><em>Item 58: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors</em></li> <li><em>Item 59: Avoid unnecessary use of checked exceptions</em></li> <li><em>Item 60: Favor the use of standard exceptions</em></li> <li><em>Item 61: Throw exceptions appropriate to the abstraction</em></li> <li><em>Item 62: Document all exceptions thrown by each method</em></li> </ul></li> </ul> <hr> <h3>Workaround "solution"</h3> <p>If a redesign is impossible, then wrapping your <code>CustomException</code> in a <code>RuntimeException</code> (or its subclass) will "work". That is, instead of:</p> <pre><code>// ideal solution, not possible without redesign @Override public static void someMethod() throws CustomException { throw new CustomException(); } //... try { someMethod(); } catch (CustomException e) { handleCustomException(e); } </code></pre> <p>You can, should you insist, do the following:</p> <pre><code>// workaround if redesign is not possible // NOT RECOMMENDED! @Override public static void someMethod() { throw new RuntimeException(new CustomException()); } //... try { someMethod(); } catch (RuntimeException e) { // not catch(CustomException e) if (e.getCause() instanceof CustomException) { handleCustomException((CustomException) e.getCause()); } else { throw e; // preserves previous behavior } } </code></pre> <p>It needs to be reiterated that this is <em>NOT</em> a recommendable technique in general. You should fix the problem at the design level if at all possible, but barring that, this is indeed a possible workaround.</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