← Back to Week 3 Hub

try/catch Flow

A runtime error normally crashes your program. A try/catch block lets you intercept the problem and keep running.

1. Syntax Errors

You broke the rules of the Java language. Missing semicolon, typo in a keyword, unclosed brace.

Caught by: the compiler, before the program runs. You can't even launch it.

2. Logic Errors

The code runs fine — it just produces the wrong answer. Java has no idea anything is wrong; it's doing exactly what you told it to do.

Caught by: you, reading the output. This is where the debugger helps.

3. Runtime Errors

The program started fine but then hit something unexpected — bad input, a missing file, an out-of-range index. Java throws an exception.

Caught by: a try/catch block. Without one, the program crashes.
Today's focus: runtime errors. The other two were covered earlier. Below, try the same program without a try/catch and with one — same input, different result.
Pick a Kid (workbook example, p.8)
$ java PickAKid
(click Run to start)
How try/catch routes execution
try { … }
Code runs here.
Everything works → finish normally.
Something breaks → throw an exception.
exception
thrown
catch (Exception e) { … }
Control jumps here.
You decide what to do — log it, show a friendly message, ask again.
Then execution continues past the catch.
Without try/catch: the exception keeps bubbling up until Java has nowhere else to go — the program halts and prints a stack trace.
With try/catch: you intercept the problem at the try block's boundary. Program keeps running.

Tip: press a quick-pick button to set the number, then Run. Switch modes to see the same input produce two very different outcomes.

BufferedReader Pipeline →