What is an Exception in Java?

What is an Exception?

An exception is an event that happens during the execution of a program, which interrupts its normal flow.

Rather than allowing the entire program to crash, Java gives developers a structured way to detect, report, and handle these problems gracefully.

An exception in Java is actually an object that is created and thrown by the Java runtime system when an error occurs.

Example: When an Exception Occurs

public class ExceptionExample {
public static void main(String[] args) {
int num1 = 10;
int num2 = 0;

// This line will cause an exception
int result = num1 / num2;

System.out.println("Result: " + result); // This line will not execute
}
}
  • In the above code, dividing 10 by 0 causes an ArithmeticException. Java immediately creates an exception object (ArithmeticException), and it stops the normal execution of the program.

Real-Life Example Analogy

Imagine a car with a sensor that detects low fuel. When the fuel runs low, it triggers a warning (just like an exception).

Instead of stopping the car immediately, it alerts the driver to handle the issue and gives them a chance to refuel before the vehicle stops.

Similarly, in Java, an exception alerts your program that something went wrong, and you can take action instead of allowing the entire system to fail.

Terms in Exception Handling

Below are the important terms used in exception handling.

1) Exception: An exception is an unusual or abnormal condition that interrupts the normal flow of a program.

It happens during runtime, for example, dividing a number by zero or accessing an invalid array index.

Example:

int a = 10;
int b = 0;
int result = a / b; // This line throws an ArithmeticException

2) Try Block: A try block is used to enclose the code that might cause an exception.
Any risky operation, like file reading, database access, or division, should be placed inside a try block.

Example:

try {
    int[] numbers = {1, 2, 3};
    System.out.println(numbers[5]); // May cause ArrayIndexOutOfBoundsException
}

The try block tests the code, but it doesn’t handle the exception itself. Handling happens in the catch block.

3) Catch Block: A catch block is used to handle exceptions that occur inside the try block.
It defines what to do when a specific type of exception happens.

Example:

try {
    int x = 5 / 0;
} catch (ArithmeticException e) {
    System.out.println("Error: Division by zero is not allowed!");
}

When an exception is thrown, Java jumps to the corresponding catch block to handle it.

4) Finally Block: The finally block is always executed, whether an exception occurs or not.
It is mostly used for cleanup tasks such as closing files, releasing database connections, or freeing resources.

Example:

try {
    int num = 50 / 10;
    System.out.println("Division successful!");
} catch (Exception e) {
    System.out.println("Something went wrong.");
} finally {
    System.out.println("This block always executes.");
}

Output:

Division successful!
This block always executes.

The finally block runs every time, even if there’s a return statement inside try or catch.

5) Throw: The throw keyword is used to manually throw an exception inside a method or block of code.

It helps developers to trigger custom exceptions when certain conditions are not met.

Example:

public class ThrowExample {
    public static void main(String[] args) {
        int age = 15;
        if (age < 18) {
            throw new ArithmeticException("Access denied - You must be 18 or older.");
        } else {
            System.out.println("Access granted!");
        }
    }
}

6) Throws: The throws keyword is used in a method declaration to specify which exceptions might occur during execution.

It tells the calling method that it must handle or declare those exceptions.

Example:

public void readFile() throws IOException {
    FileReader file = new FileReader("data.txt");
}

    Types of Exceptions in Java

    Java exceptions are categorized into two main types:

    1. Checked Exceptions

    Checked exceptions are those exceptions that are checked at compile time. This means the compiler ensures that your code either handles these exceptions using a try-catch block or declares them using the throws keyword.

    Checked exceptions usually represent external or predictable problems, such as file handling errors, database connection issues, or network problems, things that can happen even when the code is logically correct.

    It has some major points like:

    • Checked at compile time.
    • Must be handled using try-catch or declared using throws.
    • Examples:
      • IOException
      • SQLException

    For example:

    import java.io.FileReader;
    import java.io.IOException;

    public class CheckedExample {
    public static void main(String[] args) {
    try {
    FileReader file = new FileReader("data.txt"); // May not exist
    System.out.println("File opened successfully!");
    } catch (IOException e) {
    System.out.println("Error: File not found or cannot be opened.");
    }
    }
    }

    2. Unchecked Exceptions

    Unchecked exceptions are those exceptions that are checked at runtime, not at compile time. This means the compiler doesn’t require you to handle them.

    They usually occur due to logical or programming mistakes, such as dividing by zero, accessing a null object, or invalid array indexing.

    This includes some major points:

    • Checked at runtime.
    • Caused by programming mistakes.
    • Examples:
      • ArithmeticException
      • NullPointerException
      • ArrayIndexOutOfBoundsException

    For example: Unchecked Exception

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

    try {
    System.out.println(numbers[5]); // Invalid index
    } catch (ArrayIndexOutOfBoundsException e) {
    System.out.println("Error: Tried to access an invalid array index!");
    }

    System.out.println("Program continues normally...");
    }
    }
    • Here, the program tries to access an index that doesn’t exist in the array.

    Java Exception Class Hierarchy

    In Java, all exceptions and errors are part of a single family tree that begins with the Throwable class.

    This hierarchy helps developers to understand how different types of problems are categorized and handled within a program.

    The hierarchy divides problems into two main branches: Error and Exception.

    1) The Root Class – Throwable

    At the top of the hierarchy is the Throwable class, which belongs to the java.lang package.

    Every error or exception in Java is a subclass of Throwable.

    It provides two important methods that are common to both errors and exceptions:

    • getMessage() – Returns a description of the error.
    • printStackTrace() – Displays where the error occurred in the code.

    2) Error – Serious System-Level Problems

    The Error class represents serious problems that usually occur because of system or resource failures.

    These are beyond the control of the programmer and are not meant to be caught or handled in most cases.

    Errors indicate problems that can’t be fixed while the program is running, for example, when memory runs out or the JVM crashes.

    For example:

    public class ErrorExample {
    public static void main(String[] args) {
    try {
    causeError();
    } catch (Error e) {
    System.out.println("Caught an error: " + e);
    }
    }

    static void causeError() {
    throw new OutOfMemoryError("Not enough memory!");
    }
    }
    • Here, an OutOfMemoryError is manually thrown to simulate a memory problem.

    3) Exception – Recoverable Issues

    The Exception class represents problems that can occur during program execution but can be handled.

    Incorrect logic, invalid inputs, or missing resources often cause exceptions.

    Developers can use try-catch blocks to manage these issues and prevent the program from crashing.

    Java Exception Hierarchy Diagram

    What is Custom Exceptions in Java?

    In Java, not all error situations can be represented using the built-in exception classes like NullPointerException, IOException, or ArithmeticException.

    Sometimes, your program may encounter a situation that is specific to your application’s logic.

    A Custom Exception allows developers to create their own exception types by extending the Exception class (or sometimes RuntimeException).

    Example 1: Custom Checked Exception

    // Step 1: Create a custom exception class
    class InvalidAgeException extends Exception {
    public InvalidAgeException(String message) {
    super(message); // Pass the message to the Exception class constructor
    }
    }

    // Step 2: Use the custom exception in your code
    public class CustomExceptionExample {
    public static void main(String[] args) {
    try {
    checkEligibility(15); // Testing with invalid age
    } catch (InvalidAgeException e) {
    System.out.println("Caught Exception: " + e.getMessage());
    }
    }

    // Step 3: Define a method that throws the custom exception
    public static void checkEligibility(int age) throws InvalidAgeException {
    if (age < 18) {
    throw new InvalidAgeException("Age must be 18 or above to register.");
    } else {
    System.out.println("Registration successful!");
    }
    }
    }

    Output:

    Caught Exception: Age must be 18 or above to register.

    Example 2: Custom Unchecked Exception

    You can also create custom exceptions by extending RuntimeException if you don’t want to force try-catch blocks.

    // Custom unchecked exception
    class NegativeNumberException extends RuntimeException {
    public NegativeNumberException(String message) {
    super(message);
    }
    }

    public class UncheckedCustomException {
    public static void main(String[] args) {
    calculateSquare(-5);
    }

    public static void calculateSquare(int number) {
    if (number < 0) {
    throw new NegativeNumberException("Number cannot be negative.");
    }
    System.out.println("Square: " + (number * number));
    }
    }

    Output:

    Exception in thread "main" NegativeNumberException: Number cannot be negative.

    Also Learn Important Topics of Java

    Leave a Comment