Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It tells the clients of your class that the DoBlah method can throw a BlahException or <em>any other exception that extends it</em>. </p> <p>If it's a checked exception, the compiler will require that they wrap calls to this method in a try/catch block. If it's unchecked, they can choose to not catch the exception, but they have to be aware that if they don't it'll be bubbled further up the call stack.</p> <p>It doesn't say anything about unchecked exceptions like NullPointException or errors. Those can always be thrown as well. They aren't required in the throws clause.</p> <p>This code shows how it works: </p> <p>ExceptionDemo.java:</p> <pre><code>package exceptions; public class ExceptionDemo { public static void main(String[] args) { ExceptionDemo demo = new ExceptionDemo(); try { // Removing the try/catch will result in a compilation error demo.doChecked(); } catch (CheckedException e) { e.printStackTrace(); } // Note: Not inside a try/catch, in spite of the throws clause demo.doUnchecked(); } public void doChecked() throws CheckedException { System.out.println("doing something that may throw a checked exception"); } // Note: "throws" clause is unnecessary for an unchecked exception public void doUnchecked() throws UncheckedException { System.out.println("doing something that may throw an unchecked exception"); } } </code></pre> <p>CheckedException.java: </p> <pre><code>package exceptions; public class CheckedException extends Exception { public CheckedException() { super(); } public CheckedException(String message) { super(message); } public CheckedException(String message, Throwable cause) { super(message, cause); } public CheckedException(Throwable cause) { super(cause); } } </code></pre> <p>UncheckedException.java: </p> <pre><code>package exceptions; public class UncheckedException extends RuntimeException { public UncheckedException() { super(); } public UncheckedException(String message) { super(message); } public UncheckedException(String message, Throwable cause) { super(message, cause); } public UncheckedException(Throwable cause) { super(cause); } } </code></pre>
    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. 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