Throwing, catching and propagating an exception

Java's exception mechanism: throw, try, catch, finally, climbing the call stack to the first compatible catch, the order of catch blocks and try-with-resources.

  • 25 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

  • Throwing an exception (throw): what is it?

    Show the answer

    The statement that throws an exception object, most often created on the spot with new, and immediately interrupts the running code to signal that a problem occurred.

    throw new IllegalArgumentException("amount: -3");: the following statements do not run, and Java looks for a compatible catch.

  • What does this program print?

    ```java
    static void convert() {
    System.out.println("convert");
    Integer.parseInt("x");
    System.out.println("converted");
    }
    static void read() {
    try {
    convert();
    System.out.println("read ok");
    } catch (NumberFormatException e) {
    System.out.println("caught");
    } finally {
    System.out.println("finally");
    }
    System.out.println("after");
    }
    public static void main(String[] args) {
    read();
    System.out.println("end");
    }
    `

    • convert, caught, finally, after, end
    • convert, converted, read ok, finally, after, end
    • convert, caught, after, end
    • convert, caught, finally, end
    Show the answer

    convert, caught, finally, after, end

    parseInt throws NumberFormatException: converted and read ok are skipped. The catch handles the exception, the finally runs, and since the exception is caught, read ends normally: after and end are printed.

  • When several catch blocks follow each other, the … one whose type is compatible runs; the most … types are therefore placed before the more general ones.

    Show the answer

    When several catch blocks follow each other, the first one whose type is compatible runs; the most specific types are therefore placed before the more general ones.

    Java tries the catch blocks from top to bottom and stops at the first compatible one: a general catch placed first would intercept everything the following ones were meant to receive.

  • Catching an exception (catch): what is it?

    Show the answer

    The block that receives an exception of a compatible type, thrown in the associated try block, and runs the handling code.

    A catch receives its type and its subclasses: catch (RuntimeException e) catches an IllegalStateException, but not an IOException.

  • A method wraps some processing in try { … } catch (Exception e) { }. In production, the saved data is wrong and no message appears anywhere. What is the cause?

    • catch (Exception e) does not receive exceptions thrown by the method itself
    • The try block is skipped at run time when the catch holds no statement
    • The empty catch swallows the exception: the error is caught but never handled or logged
    • Caught exceptions are always passed on to the finally, which has to print them
    Show the answer

    The empty catch swallows the exception: the error is caught but never handled or logged

    An empty catch makes the problem vanish without solving it: the program carries on in an inconsistent state. At the very least log the exception, or let it climb to someone who can handle it.