Checked and unchecked exceptions

Java's Throwable hierarchy (Error, Exception, RuntimeException), the catch-or-declare rule, the throws clause and the choice of type when you write your own exception.

  • 26 cards
  • 5 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. Scenarios
  5. Understand

A few cards from the deck

  • Checked exception: what is it?

    Show the answer

    An exception the compiler forces you to deal with: any call that can throw it must be wrapped in a compatible catch or declare it in the signature.

    It is any subclass of Exception that is not a RuntimeException: IOException, SQLException, InterruptedException. Without handling, javac rejects the code.

  • javac rejects this code with unreported exception FileNotFoundException; must be caught or declared to be thrown. Which fix is valid, without changing the signature of open?

    ```java
    static void open() {
    new FileReader("a.txt");
    }
    `

    • Wrap the call in a try with catch (FileNotFoundException e)
    • Write throw in front of new FileReader("a.txt")
    • Wrap the call in a try with catch (RuntimeException e)
    • Declare throws IOException on main, which calls open
    Show the answer

    Wrap the call in a try with catch (FileNotFoundException e)

    The catch must target FileNotFoundException or one of its checked parents (IOException, Exception). RuntimeException is not one of them, and throw raises an exception instead of handling one.

  • RuntimeException and its subclasses are …; the other subclasses of Exception, like IOException, are ….

    Show the answer

    RuntimeException and its subclasses are unchecked; the other subclasses of Exception, like IOException, are checked.

    The line runs through RuntimeException: it, and not Exception, marks the exceptions the compiler does not check.

  • Unchecked exception: what is it?

    Show the answer

    An exception whose handling the compiler does not impose: RuntimeException and its subclasses, as well as Error and its own.

    NullPointerException, ArithmeticException or IllegalArgumentException can show up almost anywhere: handling them at every call would drown the code. You fix the bug instead.

  • A method loads a configuration file. If it is missing, the caller can offer another path. Which type of exception should you write for this case?

    • A class extending Error, since the file lies outside of the program itself
    • An unchecked exception, because in practice no caller will write a try block for it
    • A checked exception extending Exception: the caller can react and the compiler makes it do so
    • A RuntimeException, because files are only read at the moment the program runs
    Show the answer

    A checked exception extending Exception: the caller can react and the compiler makes it do so

    Choose checked when the caller can reasonably recover. For a caller's bug (a negative argument, a null reference), throw an unchecked exception such as IllegalArgumentException instead. Error is reserved for the virtual machine.