Lesson 45 of 50 – Java Try-Catch
90%

Java Try-Catch

The try-catch statement in Java is used to handle exceptions. The try block contains code that may cause an exception, while the catch block handles a matching exception.

1. What Is Try-Catch?

Without exception handling, an uncaught exception can terminate the current thread. A try-catch statement lets a program respond to certain exceptions and continue where appropriate.

class Main {
    public static void main(String[] args) {
        try {
            int result = 10 / 0;
            System.out.println(result);
        } catch (ArithmeticException e) {
            System.out.println("Cannot divide by zero");
        }

        System.out.println("Program continues");
    }
}
Output:
Cannot divide by zero
Program continues
2. Syntax of Try-Catch
try {
    // Code that may throw an exception
} catch (ExceptionType e) {
    // Handle the exception
}

Important points:

  • The try block contains the risky operation.
  • The catch block specifies the exception type it handles.
  • A try statement must have at least one catch block or a finally block.
  • Execution continues after the try-catch statement if the exception is handled.
3. Handling ArithmeticException

ArithmeticException is an unchecked exception. Integer division by zero is a common cause.

class Main {
    public static void main(String[] args) {
        int a = 20;
        int b = 0;

        try {
            System.out.println(a / b);
        } catch (ArithmeticException e) {
            System.out.println("Invalid division");
        }
    }
}
Output:
Invalid division

The catch block handles the exception instead of allowing it to remain uncaught.

4. Handling ArrayIndexOutOfBoundsException

This exception occurs when code accesses an array using an invalid index.

class Main {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30};

        try {
            System.out.println(numbers[5]);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Invalid array index");
        }
    }
}
Output:
Invalid array index

The valid indexes for this array are 0, 1, and 2.

5. Handling NullPointerException

A NullPointerException can occur when code tries to use an object reference that is null.

class Main {
    public static void main(String[] args) {
        String name = null;

        try {
            System.out.println(name.length());
        } catch (NullPointerException e) {
            System.out.println("Name is not initialized");
        }
    }
}
Output:
Name is not initialized

In real applications, it is usually better to prevent unexpected null values where possible, rather than relying only on exception handling.

6. Using Multiple Catch Blocks

Multiple catch blocks can handle different exception types from the same try block.

class Main {
    public static void main(String[] args) {
        try {
            int[] numbers = {10, 20, 30};
            System.out.println(numbers[5]);
        } catch (ArithmeticException e) {
            System.out.println("Arithmetic error");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Array index error");
        } catch (Exception e) {
            System.out.println("Other error");
        }
    }
}
Output:
Array index error
Place specific exception types before general exception types. Otherwise, a general catch block may make a later catch unreachable.
7. Displaying the Exception Message

The exception object provides methods such as getMessage() to retrieve its message.

class Main {
    public static void main(String[] args) {
        try {
            int result = 10 / 0;
        } catch (ArithmeticException e) {
            System.out.println(e.getMessage());
        }
    }
}

The message describes the exception. Its exact wording can depend on the Java runtime, so do not rely on a particular message for program logic.

You can also print the exception type:

System.out.println(e.getClass().getSimpleName());

For this example, the exception type is:

ArithmeticException
8. Try-Catch with User Input

Scanner's nextInt() can throw InputMismatchException if the entered value is not an integer.

import java.util.Scanner;
import java.util.InputMismatchException;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        try {
            System.out.print("Enter an integer: ");
            int number = sc.nextInt();

            System.out.println("Number: " + number);
        } catch (InputMismatchException e) {
            System.out.println("Please enter a valid integer");
        } finally {
            sc.close();
        }
    }
}
Example Output:
Enter an integer: hello
Please enter a valid integer

The finally block closes the Scanner whether the input is valid or an exception is handled. This example ends after handling invalid input.

9. Nested Try-Catch

A try-catch statement can be placed inside another try block. This is called nested try-catch.

class Main {
    public static void main(String[] args) {
        try {
            try {
                int result = 10 / 0;
            } catch (ArithmeticException e) {
                System.out.println("Division error");
            }

            int[] numbers = {10, 20};
            System.out.println(numbers[5]);

        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Array index error");
        }
    }
}
Output:
Division error
Array index error

The inner catch handles the division exception. The outer catch handles the array index exception.

10. Try-Catch with Finally

The finally block is commonly used for cleanup operations and generally runs whether or not an exception is thrown.

class Main {
    public static void main(String[] args) {
        try {
            int result = 10 / 0;
            System.out.println(result);
        } catch (ArithmeticException e) {
            System.out.println("Cannot divide by zero");
        } finally {
            System.out.println("Execution finished");
        }
    }
}
Output:
Cannot divide by zero
Execution finished

In production code, try-with-resources is often preferred for automatically closing files, streams, and other AutoCloseable resources.

11. Try-Catch in a Method

You can place exception handling inside a method to manage errors close to the operation that causes them.

class Calculator {
    void divide(int a, int b) {
        try {
            int result = a / b;
            System.out.println("Result: " + result);
        } catch (ArithmeticException e) {
            System.out.println("Cannot divide by zero");
        }
    }
}

class Main {
    public static void main(String[] args) {
        Calculator c1 = new Calculator();

        c1.divide(20, 5);
        c1.divide(20, 0);
    }
}
Output:
Result: 4
Cannot divide by zero

Each call handles its own division error, allowing the second call to execute.

12. Common Mistakes in Try-Catch
  • Catching a broad Exception when a specific exception would be more appropriate.
  • Writing an empty catch block and silently ignoring the problem.
  • Placing a general catch before a more specific catch.
  • Assuming catch handles every possible error, including all Error subclasses.
  • Using exceptions instead of validating ordinary user input.
Better Example
class Main {
    public static void main(String[] args) {
        int a = 20;
        int b = 0;

        if (b != 0) {
            System.out.println(a / b);
        } else {
            System.out.println("Divisor must not be zero");
        }
    }
}
Output:
Divisor must not be zero

When the condition is expected and easy to check, validating it directly can be clearer than deliberately triggering an exception.

13. Practice Assignments
  1. Write a program to handle division by zero using try-catch.
  2. Create an array and handle an invalid index.
  3. Handle a NullPointerException in a simple example.
  4. Use multiple catch blocks for different exception types.
  5. Write a program that accepts an integer using Scanner and handles invalid input.
  6. Demonstrate nested try-catch.
  7. Use a finally block to display a cleanup message.
  8. Create a Calculator class whose divide() method handles division errors.
Practice Tip: Test your programs with valid inputs, invalid inputs, and boundary values to understand exception handling.

🧠 Quick Quiz

1. Which block handles a matching exception?




2. Which exception commonly occurs when dividing an integer by zero?




3. Which block is commonly used for cleanup?