Glossary#
| Term | Plain meaning | Chapter |
|---|---|---|
| exception / throw | an error object interrupting execution / raising it | 1 |
| propagate | the exception moving up the call stack | 1 |
| stack trace | the list of calls active when it was thrown | 1 |
| try / catch / finally | protected block / handler / always-run clean-up | 2 |
| multi-catch | catch (A | B e) | 2 |
| swallowing | catching and ignoring an exception | 2 |
| Throwable / Error / Exception | root / JVM-level problems / program-level problems | 3 |
| checked exception | must be caught or declared (IOException) | 3 |
| unchecked exception | RuntimeException and subclasses (NullPointerException) | 3 |
throw vs throws | throw an object now / declare in a signature | 3 |
| resource / AutoCloseable | must be closed / interface with close() | 4 |
| try-with-resources | automatic closing in reverse order | 4 |
| suppressed exception | a close() failure attached to the main one | 4 |
| custom exception | your own exception class | 5 |
| wrapping / cause / chaining | translating while keeping the original ("Caused by") | 5 |
| fail fast | reject bad input immediately | 5 |
| raw type | generic type used without a type argument | 6 |
| generics / type parameter | classes/methods for any type / the T placeholder | 6, 7 |
diamond <> | inferred type arguments | 7 |
| generic method | static <T> ... with its own type parameter | 7 |
| bounded type | <T extends X> restricting T | 8 |
| type erasure | generic types removed after compile-time checking | 9 |
| reified / covariant (arrays) | keep type at run time / String[] is an Object[] | 9 |
| ArrayStoreException | wrong type stored into an array at run time | 9 |
| invariance | List<Integer> is not a List<Number> | 10 |
wildcard ? / ? extends / ? super | unknown type / read-only producer / write consumer | 10 |
| PECS | producer extends, consumer super | 10 |
Cheat Sheet#
Exceptions
try (var br = new BufferedReader(new FileReader(path))) { // auto-closed
...
} catch (FileNotFoundException e) { // specific first
...
} catch (IOException e) {
throw new QuizStorageException("could not load " + path, e); // wrap WITH cause
}
Objects.requireNonNull(arg, "arg"); if (x < 0) throw new IllegalArgumentException("x < 0: " + x); checked: IOException, SQLException, InterruptedException → catch or `throws`
unchecked: NPE, IllegalArgument/State, IndexOutOfBounds, NumberFormat, ClassCast, Arithmetic
never: empty catch · return in finally · exceptions for normal flow · wrap without causeGenerics
class Box<T> { T get(); } record Pair<A, B>(A first, B second) {}
static <T> T first(List<T> xs) static <T extends Comparable<T>> T maxOf(List<T> xs)
double sum(List<? extends Number> src) void fill(List<? super Integer> dst) // PECS can't: new T() · new T[] · instanceof List<String> · List<int> · overload by type argument
List<Integer> ≠ List<Number> (invariant) · arrays covariant → ArrayStoreExceptionTry This Yourself#
Read five stack traces. Deliberately cause: a NullPointerException, an ArrayIndexOutOfBoundsException, a NumberFormatException, an ArithmeticException (
1 / 0) and a ClassCastException. For each, point to the first line in your own code.Resources. Write a class implementing
AutoCloseablewhoseclose()also throws. Use it in try-with-resources where the body throws too. Printe.getMessage()ande.getSuppressed()[0].getMessage().Custom exception. Add
QuizNotFoundException(unchecked) with aquizIdfield. WritefindQuiz(String id)that throws it, and a caller that catches it and prints "Quizdoesn't exist". Generic utilities. Write
static <T> void swap(T[] arr, int i, int j),static <K, V> Map<V, K> invert(Map<K, V> m), andstatic <T extends Comparable<T>> boolean isSorted(List<T> xs). Try callingisSortedwith a list of a class that isn'tComparableand read the compile error.