How to Catch a Specific Exception in Java
When something goes wrong in Java, the code throws an exception, and the program stops unless a catch handles it. A catch that names one exception type handles the failure you expected and lets every other one through, so real bugs still show up.
Syntax
try {
<code that may throw>
} catch (<ExceptionType> <e>) {
<handle it, for example with <e>.getMessage()>
}
| Part | What it does |
|---|---|
try { ... } | Runs the code that may throw. |
catch (<ExceptionType> <e>) | Runs only for that type, or a subclass of it. |
<e>.getMessage() | The exception's description. |
The Java Language Specification, section 14.20: "If a value is thrown and the try statement has one or more catch clauses that can catch it, then control will be transferred to the first such catch clause."
The Example
The example turns text into a number with Integer.parseInt. For forty-two that throws a NumberFormatException; the catch prints the reason and returns 0.
Prerequisites
- A JDK, the Java Development Kit.
java Main.javaruns a single file without compiling it first. This guide uses OpenJDK 25.0.5.1.
Step 1: Write the Code
Create a file named Main.java:

public class Main {
static int toNumber(String text) {
try {
return Integer.parseInt(text);
} catch (NumberFormatException e) {
System.out.println("Not a number: " + e.getMessage());
return 0;
}
}
public static void main(String[] args) {
System.out.println(toNumber("42"));
System.out.println(toNumber("forty-two"));
}
}
Step 2: Run It
java Main.java
42
Not a number: For input string: "forty-two"
0

The first call never reached the catch. The second threw, the catch printed the message, and the program carried on. The command is the same in zsh, bash and PowerShell.
Catching a Superclass
Exceptions form a family tree: NumberFormatException extends IllegalArgumentException, which extends RuntimeException. A catch (IllegalArgumentException e) therefore catches a NumberFormatException too, along with its other subclasses. Catch the specific type when only that failure is expected, and a superclass when a whole family should be handled the same way.
When you catch both, list the subclass first; the compiler refuses a catch that can never run.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Catching the superclass above the subclass | error: exception NumberFormatException has already been caught | Put the specific type first |
Catching Exception around a lot of code | Real bugs are handled as if they were the expected failure | Catch the specific type, around as little code as possible |
An empty catch block | The failure disappears silently | At least print or log e.getMessage() |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
The program still stops with Exception in thread "main" | The exception isn't the type you catch | Read the type on that line and catch it |
has already been caught | A broader catch sits above | Reorder the clauses |
FAQ
Can one catch handle several types?
Yes, with |: catch (NumberFormatException | NullPointerException e).
Do I have to catch every exception?
Only checked exceptions, such as IOException, must be caught or declared. NumberFormatException is unchecked, so catching it is your choice.
Conclusion
catch (SomeException e) handles one kind of failure and gives you e.getMessage(); everything else keeps going up. List subclasses before superclasses.
Additional Resources
- JLS 14.20: The try statement, in the Java Language Specification.
- Integer.parseInt, in the JDK 25 API documentation.
