Java try-catch
In this page:
try {
// risky code
} catch (ExceptionType e) {
// handle e
}
The try Block
The try block wraps the specific lines of code you suspect might throw an exception. Java only starts checking for exceptions once execution enters this block; code outside it isn't protected.
Example: The try Block
public class Main {
public static void main(String[] args) {
try {
int result = 10 / 0; // suspected risky code
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("Error caught");
}
}
}
Login to try C/C++/Java/PHP code in the editor
The catch Block
A catch block catches one exception type and runs its recovery logic if a matching exception is thrown inside the paired try. If no exception occurs, the entire catch block is skipped.
Example: The catch Block
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Try code that might throw an exception
try {
// Declare result and set it to 10 / 2
int result = 10 / 2;
// Print a line to the console
System.out.println(result);
// Handle ArithmeticException if it was thrown
} catch (ArithmeticException e) {
// Print a line to the console
System.out.println("This is skipped: no exception occurred");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Multiple Catch Blocks
You can chain several catch blocks after one try to handle different exception types differently — a FileNotFoundException might prompt the user to pick a new file, while an IOException might just log and retry.
Java checks them top to bottom and runs only the first match.
Example: Multiple Catch Blocks
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Try code that might throw an exception
try {
// Declare arr and set it to new int[2]
int[] arr = new int[2];
// Print a line to the console
System.out.println(arr[5]);
// Handle ArrayIndexOutOfBoundsException if it was thrown
} catch (ArrayIndexOutOfBoundsException e) {
// Print a line to the console
System.out.println("Index error");
// Handle NullPointerException if it was thrown
} catch (NullPointerException e) {
System.out.println("Null error");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Multi-Catch Block
A multi-catch block (catch (IOException | SQLException e)) lets one block handle several unrelated exception types identically, avoiding duplicated recovery code when the response would be the same either way.
The caught variable's type is the common supertype of the listed exceptions.
Example: Multi-Catch Block
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Try code that might throw an exception
try {
// Throw NumberFormatException to signal an error
throw new NumberFormatException("bad number");
// Handle NumberFormatException | NullPointerException if it was thrown
} catch (NumberFormatException | NullPointerException e) {
// Print a line to the console
System.out.println("Handled: " + e.getMessage());
}
}
}
Login to try C/C++/Java/PHP code in the editor
Nested try-catch Blocks
You can nest a try-catch inside another try block when an inner operation needs its own, more specific error handling separate from the outer block's broader recovery logic.
This is common when a single risky call sits inside a larger risky operation.
Example: Nested try-catch Blocks
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Try code that might throw an exception
try {
// Try code that might throw an exception
try {
// Declare result and set it to 10 / 0
int result = 10 / 0;
// Handle ArithmeticException if it was thrown
} catch (ArithmeticException e) {
// Print a line to the console
System.out.println("Inner catch");
}
// Handle Exception if it was thrown
} catch (Exception e) {
System.out.println("Outer catch");
}
}
}
Login to try C/C++/Java/PHP code in the editor
- Catching a parent exception before a child one, which is a compile error
already been caught. - Catching an exception that the
tryblock can never throw (a checked one), which is a compile error. - Using a
tryblock withoutcatchorfinally, which is a compile error.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: