Java try, catch, finally – Complete Guide with Examples


Learn Java try, catch, and finally blocks to handle exceptions gracefully, ensure cleanup with finally, and write robust and error-free Java programs.

try, catch, and finally in Java – Complete Detailed Tutorial

Java provides a mechanism to handle runtime exceptions using try, catch, and finally blocks.

This ensures that program execution continues even when an error occurs.

1. try Block

  1. Contains code that might throw an exception
  2. Must be followed by at least one catch or finally block

Syntax:


try {
// code that may throw exception
}

Example:


try {
int result = 10 / 0; // ArithmeticException
}

2. catch Block

  1. Handles the exception thrown in try block
  2. Takes an exception parameter
  3. Can have multiple catch blocks for different exception types

Syntax:


catch (ExceptionType e) {
// handle exception
}

Example:


try {
int result = 10 / 0; // may throw ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero: " + e.getMessage());
}

3. finally Block

  1. Always executes whether an exception occurs or not
  2. Used for cleanup operations (like closing files, releasing resources)
  3. Optional, but recommended when using resources

Syntax:


finally {
// cleanup code
}

Example:


public class Main {
public static void main(String[] args) {
try {
int[] arr = {1, 2, 3};
System.out.println(arr[5]); // ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Index out of range: " + e.getMessage());
} finally {
System.out.println("This block always executes");
}
}
}

Output:


Index out of range: 5
This block always executes

4. Multiple catch Blocks

  1. Catch different types of exceptions separately
  2. Useful for specific handling

public class Main {
public static void main(String[] args) {
try {
int a = 10 / 0; // ArithmeticException
int[] arr = {1, 2};
System.out.println(arr[5]); // ArrayIndexOutOfBoundsException
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index is invalid");
} finally {
System.out.println("Program executed finally block");
}
}
}

Output:


Cannot divide by zero
Program executed finally block

5. try-with-resources (Java 7+)

  1. Automatically closes resources like files or streams
  2. Implements AutoCloseable interface

Example:


import java.io.*;

public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
String line = br.readLine();
System.out.println(line);
} catch (IOException e) {
System.out.println("File error: " + e.getMessage());
}
}
}
  1. No need to explicitly close the BufferedReader

6. Key Points

  1. try block: risky code
  2. catch block: handle exceptions
  3. finally block: executes always, used for cleanup
  4. Multiple catch: handle different exception types
  5. try-with-resources: automatically closes resources