← Back to Core Java Course | Chapter 10: Exception Handling | Lesson 2 of 8

Java try-catch

try-catch is like a safety net: you try something risky and, if it fails, catch the problem and respond calmly. The program does not crash.
Syntax
java
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

java
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");
		}
	}
}

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

java
// 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");
		}
	}
}

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

java
// 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");
		}
	}
}

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

java
// 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());
		}
	}
}

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

java
// 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");
		}
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Catching a parent exception before a child one, which is a compile error already been caught.
  2. Catching an exception that the try block can never throw (a checked one), which is a compile error.
  3. Using a try block without catch or finally, which is a compile error.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.