Command Palette

Search for a command to run...

Phase 7Intermediate8 of 17 in Core Java

Exceptions

Handle failure like a professional: exceptions, try/catch/finally, checked vs unchecked, custom exceptions, try-with-resources and helpful NPEs.

Programs meet trouble all the time: a file isn't there, a number won't parse, the network drops, a value is null when nobody expected it. Java's answer is the exception: an object that describes what went wrong, thrown at the point of failure and caught by code that knows what to do about it. Done well, exceptions keep the happy path readable and make failures loud and easy to diagnose. Done badly, they swallow bugs, leak files and connections, and turn a one-line fix into a day of log-reading.

This phase starts with what an exception really is (an object on the heap, a walk up the call stack, a table in the bytecode), then the try/catch/finally statement, the checked/unchecked split that makes Java unique, throw and throws, writing your own exception types, try-with-resources (Java 7) for closing things safely, multi-catch and precise rethrow, and finally the habits senior engineers rely on, including the helpful NullPointerException messages added in Java 14.

It builds on Phase 5 (inheritance and polymorphism: the exception hierarchy is a class hierarchy, and catch matches by subtype) and Topic 3.x's call stack. Later phases lean on it constantly: collections throw ConcurrentModificationException (Topic 9.9), streams and lambdas complicate checked exceptions (Phase 10), and file I/O and concurrency are built around IOException and InterruptedException.

0/8 · 0%
8 topics ~4 h 32 code blocks & diagrams
Start with the first topic
1
7.1

What Exceptions Are

An exception is an object that describes something that went wrong. When it is thrown, Java stops the current code, walks back up the chain of method calls looking for a matching catch, and if none is found the thread dies and prints a stack trace.

31 min 4 code practice

2
7.2

try, catch and finally

try marks code that might fail, each catch handles one kind of exception, and finally holds clean-up code that runs whether the try finished normally, returned early, or threw.

31 min 4 code practice

3
7.3

Checked vs Unchecked Exceptions

Checked exceptions (subclasses of Exception but not RuntimeException) must be caught or declared with throws, and the compiler enforces it. Unchecked exceptions (RuntimeException, Error and their subclasses) need no declaration because they usually mean a bug or a broken environment.

30 min 4 code practice

4
7.4

throw and throws

throw is a statement that throws one exception object right now. throws is part of a method signature that warns callers which checked exceptions the method may pass up to them.

32 min 4 code practice

5
7.5

Writing Your Own Exceptions

A custom exception is a class you write that extends Exception (checked) or RuntimeException (unchecked). It gives a failure a meaningful name, can carry extra data, and should keep the original exception as its cause.

31 min 4 code practice

6
7.6

try-with-resources

try-with-resources declares objects that must be closed (files, sockets, database connections) in parentheses after try. Java closes them automatically, in reverse order, however the block ends, and keeps any failure from close() as a suppressed exception instead of losing the original error.

31 min 4 code practice

7
7.7

Multi-catch and Rethrowing

A multi-catch, catch (A | B e), handles several unrelated exception types with one block. Rethrowing passes an exception on after doing something with it, and Java 7's precise rethrow lets you catch broadly while still declaring only the exceptions that can really occur.

31 min 4 code practice

8
7.8

Exception Best Practices and Helpful NullPointerExceptions

Good exception handling means failing fast with clear messages, catching only what you can handle, never swallowing errors, keeping causes, closing resources and logging once. Since Java 14, NullPointerException messages also say exactly which value was null.

36 min 4 code practice