Site icon FahmidasClassroom

Java Exception Handling Examples

Execption

Exception handling is an important aspect of Java programming that allows you to gracefully handle unexpected errors and abnormal situations in your code. Java provides a robust mechanism for handling exceptions through the use of try-catch blocks. This tutorial will walk you through various examples of exception handling in Java to help you understand how to deal with different types of exceptions.

Basic Exception Handling

Let’s start with a simple example of a division operation that might throw an ArithmeticException if the denominator is zero. We will handle this exception using a try-catch block.

public class BasicExceptionHandling {
    public static void main(String[] args) {
        int numerator = 10;
        int denominator = 0;

        try {
            int result = numerator / denominator;
            System.out.println("Result: " + result);
        } catch (ArithmeticException e) {
            System.err.println("Exception: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

In this example, the code inside the try block attempts to divide numerator by denominator. Since denominator is set to zero, it will throw an ArithmeticException. The catch block will catch the exception, print an error message, and print the stack trace.

2. Multiple Catch Blocks

You can have multiple catch blocks to handle different types of exceptions separately. Let’s modify the previous example to handle a NumberFormatException as well.

public class MultipleCatchBlocks {
    public static void main(String[] args) {
        String numberStr = "NotANumber";

        try {
            int number = Integer.parseInt(numberStr);
            System.out.println("Number: " + number);
        } catch (NumberFormatException e) {
            System.err.println("NumberFormatException: " + e.getMessage());
            e.printStackTrace();
        } catch (ArithmeticException e) {
            System.err.println("ArithmeticException: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

In this example, we attempt to parse a non-numeric string into an integer. It will throw a NumberFormatException, which will be caught by the first catch block. If the string is successfully parsed, the catch block for ArithmeticException will not be executed.

3. Handling Multiple Exceptions with a Single Catch Block

You can also catch multiple exceptions with a single catch block using a common parent class or interface of the exception types you want to handle.

public class MultipleExceptionsInSingleCatchBlock {
    public static void main(String[] args) {
        int[] numbers = { 1, 2, 3 };
        int index = 5;

        try {
            int value = numbers[index];
            System.out.println("Value: " + value);
        } catch (ArrayIndexOutOfBoundsException | NullPointerException e) {
            System.err.println("Exception: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

In this example, we access an index that is out of bounds in the numbers array, causing an ArrayIndexOutOfBoundsException. We also use the NullPointerException by setting numbers to null. Both exceptions can be caught in a single catch block.

4. The finally Block

The finally block allows you to execute code that must run regardless of whether an exception occurred or not. It is commonly used to release resources or perform cleanup operations.

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

public class FinallyBlockExample {
    public static void main(String[] args) {
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader("file.txt"));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            System.err.println("IOException: " + e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                System.err.println("Error while closing the file: " + e.getMessage());
            }
        }
    }
}

In this example, we read data from a file using a BufferedReader. The finally block ensures that the BufferedReader is closed, even if an exception occurs during the reading process.

5. Creating Custom Exceptions

In some cases, you may need to define your own custom exception classes to handle specific errors in your application. Here’s an example of a custom exception called CustomException:

public class CustomExceptionExample {
    static class CustomException extends Exception {
        public CustomException(String message) {
            super(message);
        }
    }

    public static void main(String[] args) {
        int age = 15;

        try {
            if (age < 18) {
                throw new CustomException("You must be 18 or older to access this content.");
            } else {
                System.out.println("Access granted!");
            }
        } catch (CustomException e) {
            System.err.println("CustomException: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

In this example, we create a custom exception CustomException, and when the age is less than 18, we throw this exception with a custom message.

Conclusion

Exception handling is a critical part of Java programming as it allows you to handle errors effectively and ensure your program’s robustness. By using try-catch blocks, you can gracefully handle exceptions and guide the flow of your application when something unexpected happens. Additionally, custom exceptions can be used to handle specific error scenarios in your code. Always remember to handle exceptions appropriately to make your Java applications more reliable and user-friendly.

Exit mobile version