try / catch in Java
Catching exceptions, catch order from specific to general.
The alarm rings
Wrap risky code in **try. When something throws, Java skips the rest of the try block and jumps to the first catch whose type matches. After the catch, execution continues after the whole try statement**.
try {
int n = Integer.parseInt(text);
use(n); // skipped on error
} catch (NumberFormatException e) {
System.out.println("not a number");
}Follow the alarm
What does this print?
try {
System.out.print("1");
String s = null;
s.length();
System.out.print("2");
} catch (NullPointerException e) {
System.out.print("3");
}
System.out.println("4");134123413124
Show the answer
1 prints, s.length() throws, **2 is skipped**, the catch prints 3, then execution continues after the try with 4.
First match wins
Catches are checked top to bottom, and a catch matches its type and every subclass. The first match handles it; the rest are ignored. So put specific types before general ones: FileNotFoundException before IOException.
try {
open(file);
} catch (FileNotFoundException e) {
// specific first
} catch (IOException e) {
// then the general case
}Which catch?
5 / 0 throws ArithmeticException. What prints?
try {
int x = 5 / 0;
} catch (IllegalArgumentException e) {
System.out.println("arg");
} catch (RuntimeException e) {
System.out.println("runtime");
}argruntimearg runtime
Show the answer
ArithmeticException isn't an IllegalArgumentException, but it is a RuntimeException, so the second catch handles it. Only one catch ever runs.
General first = unreachable
Put catch (Exception e) first and it swallows every NumberFormatException before the next catch could see it. That later catch can never run, and Java treats it as a compile error.
try {
Integer.parseInt("x");
} catch (Exception e) {
} catch (NumberFormatException e) { // error
}Nobody answers the alarm
An ArrayIndexOutOfBoundsException is thrown, but the only catch is for ArithmeticException. What happens to the code after the try?
Think about it, then reveal the answer
It never runs. No catch matches, so the exception propagates out of the method to its caller, and so on. If it escapes main, the program dies with a stack trace.
Catching narrowly
Parsing user input is the classic use: catch (NumberFormatException e) shows a friendly "please enter a number" message. Catching just that type means real bugs — say, a NullPointerException in your own code — still surface instead of being mislabelled as bad input.
Key takeaways
- Statements after the throw in try are skipped
- The first matching catch wins; the rest are ignored
- Specific before general: FileNotFoundException before IOException
- Afterwards, execution continues after the whole try statement
💡 Catch blocks are safety nets stacked from fine mesh to wide mesh — the first one that fits catches the fall.
A try block costs essentially nothing when nothing is thrown: the compiler records the protected range in a method's exception table instead of adding instructions to the normal path.
Practice questions
What does this print?
try {
System.out.print("A");
int x = 1 / 0;
System.out.print("B");
} catch (ArithmeticException e) {
System.out.print("C");
}
System.out.println("D");- ACD
- ABCD
- AC
- ABD
Check your answer
ACD. A prints, 1 / 0 throws, B is skipped, the catch prints C, then execution continues with D.
What does this print?
try {
Integer.parseInt("x");
} catch (Exception e) {
System.out.println("general");
} catch (NumberFormatException e) {
System.out.println("number");
}- general
- number
- Compile error
- general number
Check your answer
Compile error. catch (Exception e) already handles every NumberFormatException, so the second catch is unreachable — a compile error.