Theory
Five keywords, one working desk
Last lesson wrote the incident report: OverdueException. Now the desk must actually use it.
The flow we want: the return-desk method detects the overdue case and raises the report; it announces on its signature that such reports can escape it; the caller wraps the risky call, handles the report, and the register closes properly no matter what happened.
Java spells those 5 duties: throw, throws, try, catch, finally. One listing shows them all.
Practical
The complete grammar in one desk
class OverdueException extends Exception {
OverdueException(String msg) { super(msg); }
}
public class ReturnDesk {
static void checkReturn(int daysLate) throws OverdueException {
if (daysLate > 14) {
throw new OverdueException("Overdue by " + daysLate + " days");
}
System.out.println("Returned within limit");
}
public static void main(String[] args) {
try {
checkReturn(20);
System.out.println("This line never runs");
} catch (OverdueException e) {
System.out.println("Caught: " + e.getMessage());
} finally {
System.out.println("Register closed");
}
}
}
Theory
Follow the jump
Trace the run with daysLate = 20:
trybegins, calls checkReturn(20)- 20 > 14, so
throwfires: checkReturn stops instantly; its remaining line never runs - the exception flies back to the caller; the rest of the try block is skipped (hence "This line never runs")
catch (OverdueException e)matches, prints Caught: Overdue by 20 daysfinallyruns: Register closed
With daysLate = 5 instead: the try completes normally, catch is skipped, and finally still runs.
Formula
throw vs throws (the 1-mark classic)
throw: a statement INSIDE a method that raises exactly one exception object, right now.
throw new OverdueException("...");
throws: a clause ON the method signature that declares what may escape, warning every caller to prepare.
static void checkReturn(int d) throws OverdueException
Memory line for the exam: throw does it, throws declares it. One acts, the other announces.
Quiz
try { System.out.print("A "); int x = 5 / 0; System.out.print("B "); } catch (ArithmeticException e) { System.out.print("C "); } finally { System.out.print("D"); } What prints?
- A B C D
- A C D
- A C
- A D
Show the answer
A C D
A prints first. Then 5 / 0 throws ArithmeticException, so B is skipped: execution jumps straight from the throwing line to the matching catch, printing C. finally always runs, adding D. Option A forgets the jump skips B. Option C forgets that finally is unconditional. Option D imagines the catch not matching, but ArithmeticException is exactly what integer division by zero throws. Result: A C D.
Think first
Does finally beat return?
A method's try block ends with return 1; and its finally prints "closing". Before tapping: does "closing" appear, or does return exit first?
Show the answer
"closing" appears. finally runs even when the try block returns, and even when an exception is flying: Java holds the pending return, executes finally, then completes the return. That guarantee is finally's whole purpose: cleanup (closing files, registers, connections) that must happen on EVERY exit path. The only escape it cannot survive is System.exit(), which kills the JVM itself. Exam phrasing: finally runs whether an exception occurs or not.
Watch out
The catch that can never run
Order multiple catch blocks child first, parent last:
catch (Exception e) { ... }
catch (OverdueException e) { ... } // unreachable!
does not compile: Exception already swallows every OverdueException, so the second catch is dead code and javac says the exception has already been caught. Put the specific handler first, the general safety net last. And never write an empty catch block: silently swallowing the report is worse than the crash.
Theory
The desk pattern, everywhere
This shape (risky call in try, specific catch, cleanup in finally) is not exam-only: it is how BookBridge survives Tuesday's "12x" too, by catching NumberFormatException around parseInt and re-asking instead of dying. You will meet the same grammar wrapping file reads in BCA504's PHP cousin, database calls in ADO.NET (BCA404 Unit 5), and thread interruptions in the very next unit. Learn the jump once; it is the same jump in every language you touch after this.
Summary
Key takeaways
- throw raises one exception object inside a method; throws declares on the signature what may escape.
- try wraps risky code; on a throw, the rest of try is skipped and control jumps to the matching catch.
- finally always runs: normal completion, exception, even a pending return; only System.exit prevents it.
- Trace pattern: A, jump, C, D: what threw, what got skipped, what handled, what always ran.
- Multiple catches go child before parent, or the parent makes later catches unreachable (compile error).
- Empty catch blocks hide problems; handle or log, never swallow.
- Memory hook: throw does it, throws declares it, finally survives it.