Five keywords, one complete failure-handling toolkit
try marks risky code, catch handles what goes wrong, throw raises a new failure, throws warns callers, and finally runs no matter what — even if the method returns early or another exception is already in flight.
After this lesson
You should be able to
- Write a try-catch block that handles a specific exception type.
- Explain the difference between throw and throws.
- Explain when finally runs, and why it is used for cleanup.
try and catch: naming the failure you expect
Code that might fail goes inside a try block. If it throws an exception, Java looks for a matching catch block immediately after — matching by the exception's type, not by which line failed. A catch (ArithmeticException e) block only catches arithmetic problems; an unrelated exception type passes straight through it, unhandled.
You can stack several catch blocks after one try, each handling a different exception type, checked in order from top to bottom — the same top-to-bottom matching instinct as an if-else-if ladder from CS105ES, just matching exception types instead of conditions.
try {
int result = 10 / userInput;
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero.");
} catch (Exception e) {
System.out.println("Something else went wrong: " + e.getMessage());
}throw creates a failure; throws warns about one
throw is an executable statement: throw new IllegalArgumentException("marks cannot be negative"); actually raises an exception right there, the moment that line runs. throws is completely different — it appears in a method's signature, not its body, and it is a declaration, not an action: public void setMarks(double m) throws InvalidMarksException tells every caller "I might fail this way, and you must acknowledge it."
A method that declares throws for a checked exception forces every caller to either catch it or declare their own throws, passing the obligation further up — the compiler checks this chain the same way it checks that every variable is declared before use.
finally: the block that always runs
A finally block, placed after all catch blocks, runs no matter what happens: whether the try succeeded, whether an exception was caught, even if the try or catch block returns early. This makes it the right place for cleanup that must always happen, like closing a file — you cannot accidentally skip it by returning early, the way you could forget a cleanup step in CS105ES's manual resource management.
This guarantee is strong enough that finally runs even when the try block contains a return statement — Java evaluates the return value, then runs finally, then actually returns, so finally is genuinely the last thing to execute before control leaves the method.
Try it yourself
Write a method withdraw(double amount) that throws a checked InsufficientFundsException if amount exceeds a fixed balance of 1000. In the calling code, wrap the call in a try-catch, and add a finally block that always prints "Transaction attempt finished."
Need a hint?
A checked exception class just needs a constructor that passes its message to the parent Exception class using super.
Check the worked solution
The finally block prints regardless of whether the withdrawal succeeds or the exception fires — that unconditional guarantee is exactly why it is the right place for a message that must appear every time, success or failure, rather than duplicating the print statement inside both the try and the catch.
class InsufficientFundsException extends Exception {
public InsufficientFundsException(String message) {
super(message);
}
}
static void withdraw(double amount) throws InsufficientFundsException {
double balance = 1000;
if (amount > balance) {
throw new InsufficientFundsException("Not enough balance");
}
System.out.println("Withdrawn: " + amount);
}
try {
withdraw(1500);
} catch (InsufficientFundsException e) {
System.out.println(e.getMessage());
} finally {
System.out.println("Transaction attempt finished.");
}Quick check
If a try block contains a return statement, does the finally block still run before the method actually returns?
Why this lesson exists
Syllabus mapping
usage of try, catch, throw, throws and finally.
Maps to course outcome CO1.