Exception Handling in Java

Exception Handling in Java

ยท

3 min read

Exception handling in Java is a mechanism that allows you to gracefully handle unexpected events or errors that can occur during the execution of your program. Exceptions represent abnormal conditions or errors that disrupt the normal flow of a program. Java provides a robust exception handling mechanism to catch, handle, and, if necessary, propagate exceptions. Here's an overview of exception handling in Java:

Types of Exceptions:

  1. Checked Exceptions:

    • Checked exceptions are exceptions that are checked at compile-time.

    • These exceptions are usually external to the Java program and are typically related to I/O operations, such as reading from a file or connecting to a network.

    • You are required to either handle checked exceptions using try-catch blocks or declare them in the method signature using the throws keyword.

  2. Unchecked Exceptions (Runtime Exceptions):

    • Unchecked exceptions are not checked at compile-time.

    • They often result from programming errors, such as dividing by zero or accessing an array element that doesn't exist.

    • You are not required to handle or declare unchecked exceptions explicitly.

Exception Handling Keywords and Constructs:

  1. try-catch Blocks:

    • You use a try block to enclose the code that might throw exceptions.

    • A catch block follows the try block and specifies the type of exception it can handle.

    • Multiple catch blocks can be used to handle different types of exceptions.

    • An optional finally block can be used to specify code that should be executed regardless of whether an exception occurs or not.

    try {
        // Code that might throw an exception
    } catch (ExceptionType1 e1) {
        // Handle ExceptionType1
    } catch (ExceptionType2 e2) {
        // Handle ExceptionType2
    } finally {
        // Optional cleanup code
    }
  1. throws Keyword:

    • You can use the throws keyword in a method signature to indicate that the method can throw specific types of exceptions.

    • It is used for checked exceptions.

    void myMethod() throws SomeException {
        // Method code that might throw SomeException
    }
  1. throw Keyword:

    • The throw keyword is used to explicitly throw an exception in your code.

    • You can create custom exceptions by extending existing exception classes or implementing your own.

    if (condition) {
        throw new MyCustomException("This is a custom exception message");
    }

Example: Handling Checked Exception

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

public class FileReadExample {
    public static void main(String[] args) {
        try {
            File file = new File("example.txt");
            FileReader reader = new FileReader(file);
            // Read file content
            reader.close();
        } catch (IOException e) {
            System.err.println("An error occurred: " + e.getMessage());
        }
    }
}

In this example, we attempt to read a file, and if an IOException occurs, we catch it and print an error message.

Example: Handling Unchecked Exception

public class DivisionExample {
    public static void main(String[] args) {
        int numerator = 10;
        int denominator = 0;
        try {
            int result = numerator / denominator; // This will throw ArithmeticException
            System.out.println("Result: " + result);
        } catch (ArithmeticException e) {
            System.err.println("Division by zero is not allowed.");
        }
    }
}

In this example, we catch an ArithmeticException that occurs when attempting to divide by zero.

Exception handling is crucial for writing reliable and robust Java applications. It allows you to gracefully handle errors, prevent crashes, and provide meaningful feedback to users when something goes wrong.

Did you find this article valuable?

Support Karun by becoming a sponsor. Any amount is appreciated!