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.
- Concepts
- Questions
- Sort and link
- Scenarios
- 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 compatiblecatch.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,endconvert,converted,read ok,finally,after,endconvert,caught,after,endconvert,caught,finally,end
Show the answer
convert,caught,finally,after,endparseIntthrowsNumberFormatException:convertedandread okare skipped. Thecatchhandles the exception, thefinallyruns, and since the exception is caught,readends normally:afterandendare 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
catchreceives its type and its subclasses:catch (RuntimeException e)catches anIllegalStateException, but not anIOException.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
tryblock is skipped at run time when thecatchholds no statement - The empty
catchswallows 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
catchswallows the exception: the error is caught but never handled or loggedAn empty
catchmakes 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.