Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To define a <strong>checked</strong> exception you create a subclass (or hierarchy of subclasses) of <a href="http://java.sun.com/javase/7/docs/api/java/lang/Exception.html" rel="noreferrer"><code>java.lang.Exception</code></a>. For example:</p> <pre><code>public class FooException extends Exception { public FooException() { super(); } public FooException(String message) { super(message); } public FooException(String message, Throwable cause) { super(message, cause); } public FooException(Throwable cause) { super(cause); } } </code></pre> <p>Methods that can potentially throw or propagate this exception must declare it:</p> <pre><code>public void calculate(int i) throws FooException, IOException; </code></pre> <p>... and code calling this method must either handle or propagate this exception (or both):</p> <pre><code>try { int i = 5; myObject.calculate(5); } catch(FooException ex) { // Print error and terminate application. ex.printStackTrace(); System.exit(1); } catch(IOException ex) { // Rethrow as FooException. throw new FooException(ex); } </code></pre> <p>You'll notice in the above example that <a href="https://docs.oracle.com/javase/7/docs/api/java/io/IOException.html" rel="noreferrer"><code>IOException</code></a> is caught and rethrown as <code>FooException</code>. This is a common technique used to encapsulate exceptions (typically when implementing an API).</p> <p>Sometimes there will be situations where you don't want to force every method to declare your exception implementation in its throws clause. In this case you can create an <strong>unchecked</strong> exception. An unchecked exception is any exception that extends <a href="http://java.sun.com/javase/7/docs/api/java/lang/RuntimeException.html" rel="noreferrer"><code>java.lang.RuntimeException</code></a> (which itself is a subclass of <a href="https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html" rel="noreferrer"><code>java.lang.Exception</code></a>):</p> <pre><code>public class FooRuntimeException extends RuntimeException { ... } </code></pre> <p>Methods can throw or propagate <code>FooRuntimeException</code> exception without declaring it; e.g.</p> <pre><code>public void calculate(int i) { if (i &lt; 0) { throw new FooRuntimeException("i &lt; 0: " + i); } } </code></pre> <p>Unchecked exceptions are typically used to denote a programmer error, for example passing an invalid argument to a method or attempting to breach an array index bounds.</p> <p>The <a href="http://java.sun.com/javase/7/docs/api/java/lang/Throwable.html" rel="noreferrer"><code>java.lang.Throwable</code></a> class is the root of all errors and exceptions that can be thrown within Java. <a href="http://java.sun.com/javase/7/docs/api/java/lang/Exception.html" rel="noreferrer"><code>java.lang.Exception</code></a> and <a href="http://java.sun.com/javase/7/docs/api/java/lang/Error.html" rel="noreferrer"><code>java.lang.Error</code></a> are both subclasses of <a href="http://java.sun.com/javase/7/docs/api/java/lang/Throwable.html" rel="noreferrer"><code>Throwable</code></a>. Anything that subclasses <a href="http://java.sun.com/javase/7/docs/api/java/lang/Throwable.html" rel="noreferrer"><code>Throwable</code></a> may be thrown or caught. However, it is typically bad practice to catch or throw <a href="http://java.sun.com/javase/7/docs/api/java/lang/Error.html" rel="noreferrer"><code>Error</code></a> as this is used to denote errors internal to the JVM that cannot usually be "handled" by the programmer (e.g. <a href="http://java.sun.com/javase/7/docs/api/java/lang/OutOfMemoryError.html" rel="noreferrer"><code>OutOfMemoryError</code></a>). Likewise you should avoid catching <a href="http://java.sun.com/javase/7/docs/api/java/lang/Throwable.html" rel="noreferrer"><code>Throwable</code></a>, which could result in you catching <a href="http://java.sun.com/javase/7/docs/api/java/lang/Error.html" rel="noreferrer"><code>Error</code></a>s in addition to <a href="http://java.sun.com/javase/7/docs/api/java/lang/Exception.html" rel="noreferrer"><code>Exception</code></a>s.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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