An exception interrupts normal execution. A try block contains work that may fail; a matching catch block handles a particular exception type. After a catch finishes normally, the program continues after the try/catch. Without a matching handler, the run ends with a stack trace showing the exception and call locations.
In catch (NumberFormatException error), the first name is the type to handle
and error is a variable holding the exception object. You can choose another
variable name. error.getMessage() reads the explanation supplied when the
exception was created. throw new IllegalArgumentException("...") creates an
exception with that message and interrupts the normal path.
NumberFormatException is useful when parsing whole-number text. Catch it near the input boundary to explain the rejected value. Do not put all program code in one enormous catch(Exception) block: that can turn a real programming defect into an apparently ordinary input message. Recover only where you know what recovery means, such as skipping one malformed record while retaining earlier good ones.
Throw when an object cannot meet its contract
A constructor can throw IllegalArgumentException rather than create an invalid object. Check required values before assigning fields. The caller then decides whether to explain the problem, retry or stop. A constructor that throws never returns a successfully initialized instance to that caller.
public class Main {
public static void main(String[] args) {
try {
int minutes = Integer.parseInt("oops");
System.out.println(minutes);
} catch (NumberFormatException error) {
System.out.println("Enter a whole number");
}
System.out.println("Still running");
}
}Check your prediction
Enter a whole number
Still runningThe print inside try is skipped because parsing throws first. The catch handles the known failure and the later message still runs. Replacing oops with 25 takes the normal path instead.
public class Main {
public static void main(String[] args) {
try {
Session session = new Session(0);
System.out.println(session.minutes());
} catch (IllegalArgumentException error) {
System.out.println(error.getMessage());
}
}
}
class Session {
private final int minutes;
Session(int minutes) {
if (minutes < 1 || minutes > 240) {
throw new IllegalArgumentException("Minutes must be 1 to 240");
}
this.minutes = minutes;
}
int minutes() {
return minutes;
}
}Check your prediction
Minutes must be 1 to 240The variable session is never initialized successfully on this path. Throwing an exception is an explicit signal, not a returned sentinel such as -1 that could be mistaken for ordinary data.
Handle the more specific failure first
NumberFormatException is a more specific kind of IllegalArgumentException. If both catches follow the same try, put NumberFormatException first: Java tries the catches in order and chooses the first matching type. A broad catch first would already include number-format failures, making the later specific catch unreachable. You do not need to write an exception class to use this relationship.
Here parsing can fail before the range check, or a parsed number can fail the range rule. Follow each call through the try and just one catch.
public class Main {
public static void main(String[] args) {
printDuration("oops");
printDuration("0");
printDuration("25");
}
static void printDuration(String text) {
try {
int minutes = Integer.parseInt(text);
if (minutes < 1 || minutes > 240) {
throw new IllegalArgumentException("Minutes must be 1 to 240");
}
System.out.println("Accepted: " + minutes);
} catch (NumberFormatException error) {
System.out.println("Not a whole number");
} catch (IllegalArgumentException error) {
System.out.println(error.getMessage());
}
}
}Check your prediction
Not a whole number
Minutes must be 1 to 240
Accepted: 25The first call reaches the first catch. The second reaches the second catch. The third reaches neither. Once a catch handles a failure, execution continues after the whole try/catch; it does not return to the failed statement inside try. The final planner uses this same distinction when explaining rejected input.
Follow the path after a throw
When a statement throws, the remaining statements in that try path do not run. Java searches for a matching catch, runs its body, then continues after the try/catch unless another failure escapes. It does not return to the failed statement and retry it automatically.
Trace a parsing call with a counter increment after it. A valid line reaches the increment; an invalid line transfers to catch first. Moving that increment before parsing would count rejected work as accepted. The location of the try matters just as much: a catch inside a loop lets later records be attempted, while a catch around the loop ends that loop on its first failure.
Catch the category you can explain and recover from. Catching every exception may hide an indexing bug as a user input problem. Keep the first unexpected failure visible while you learn to distinguish conversion errors from domain validation errors.
Debug a plausible mistake
The beginner project uses a recover-and-continue policy. An exception catch around a whole loop exits that loop on its first rejected record. The same catch inside the body can reject one record while allowing the next iteration to proceed.
Repair per-record recovery
Keep valid minutes after a malformed record. Print Rejected followed by 35. Do not replace bad text with a valid zero-minute record. Predict what the current program actually prints before changing it. Make the smallest repair, then move "bad" to the start of the array and to the end; both should still print Rejected and 35. The supplied version compiles; its behavior is the problem.
public class Main {
public static void main(String[] args) {
int total = 0;
try {
for (String line : new String[] {"25", "bad", "10"}) {
total += Integer.parseInt(line);
}
} catch (NumberFormatException expected) {
System.out.println("Rejected");
}
System.out.println(total);
}
}Hint 1: the idea
Recovery belongs at the boundary of the work you intend to skip.
Hint 2: a first step
Move the try/catch around one parsing attempt while keeping the loop outside it.
Show one solution
public class Main {
public static void main(String[] args) {
int total = 0;
for (String line : new String[] {"25", "bad", "10"}) {
try {
total += Integer.parseInt(line);
} catch (NumberFormatException expected) {
System.out.println("Rejected");
}
}
System.out.println(total);
}
}Expected output after repair:
Rejected
35The bug was where the try started: around the whole loop, the first bad record jumped to the catch and ended the loop, so 10 was never added and the program printed 25. With the try inside the loop, a catch skips only that one record and the loop carries on. With "bad" at the end, the old code would also print 35, which is why the start and middle positions are the cases that matter.
Your turn
Explain rejected numeric input
Define printMinutes(String text). Parse trimmed text, reject values outside 1..240 with the message Out of range, and print valid minutes. Catch NumberFormatException and print Not a whole number. Assume text is non-null. Call it with "25", "0" and "oops".
public class Main {
public static void main(String[] args) {
// Call printMinutes on three cases.
}
// Define the helper and handle only the expected parsing exception.
}Hint 1: the idea
Parsing failure and a parsed but invalid value need distinct paths. A guard can end the normal path after reporting invalidity.
Hint 2: a first step
Put parseInt inside try. Catch the specific exception and keep the range comparison in the successful parsing path.
Show one solution
public class Main {
public static void main(String[] args) {
printMinutes("25");
printMinutes("0");
printMinutes("oops");
}
static void printMinutes(String text) {
try {
int minutes = Integer.parseInt(text.trim());
if (minutes < 1 || minutes > 240) {
System.out.println("Out of range");
return;
}
System.out.println(minutes);
} catch (NumberFormatException error) {
System.out.println("Not a whole number");
}
}
}25
Out of range
Not a whole numberThe return after "Out of range" ends this call so the number is not printed
as well. The catch names only NumberFormatException, so it handles text that
isn't a number and nothing else.
Change it and explain
Try whitespace around a valid number, a decimal such as "2.5", and a number too large for int. Classify which failure is syntax/range at the parsing boundary and which is the session's narrower domain rule.
For an uncaught error, start with the exception message and the first relevant program frame. The browser shows your source frames and may omit the JDK's own internal frames. Do not compare exact internal stack-frame counts as part of a lesson's business rule; focus on the failing operation and your caller chain.
Going further: finally
This cleanup feature is optional for the course project.
A finally block runs as control leaves the try/catch, whether the operation succeeded or failed in the ordinary exception cases we use here. Use it for cleanup when needed, not to disguise a failed operation as a success. We do not need resources or custom exception hierarchies to understand this basic flow.
public class Main {
public static void main(String[] args) {
try {
System.out.println(Integer.parseInt("bad"));
} catch (NumberFormatException error) {
System.out.println("Rejected");
} finally {
System.out.println("Attempt finished");
}
System.out.println("Next step");
}
}Check your prediction
Rejected
Attempt finished
Next stepChange bad to 12 and predict which line disappears. finally still runs, but the catch is no longer entered. This sequence is about control flow; it is not a claim that every error in every environment is recoverable.
Completion is saved in this browser only. It does not measure mastery.