Mark As Completed Discussion

Exception Handling

In Java, exceptions are used to handle errors and unexpected events that occur during program execution. When an exception is thrown, the normal flow of execution is disrupted, and the program looks for an exception handler to handle the exception.

Types of Exceptions

Java has several built-in exception classes that represent different types of exceptions. Some common exceptions include:

  • ArithmeticException: Thrown when an illegal arithmetic operation occurs, such as dividing by zero.
  • NullPointerException: Thrown when there is an attempt to access a null object.
  • ArrayIndexOutOfBoundsException: Thrown when an array is accessed with an illegal index.

Handling Exceptions with try-catch

To handle exceptions, you can use the try-catch block. The code that might throw an exception is placed inside the try block, and the catch block is used to catch and handle the exception.

Here's an example that demonstrates exception handling:

TEXT/X-JAVA
1public class ExceptionHandlingExample {
2
3    public static void main(String[] args) {
4        try {
5            int result = divide(10, 0);
6            System.out.println("Result: " + result);
7        } catch (ArithmeticException e) {
8            System.out.println("Error: Division by zero");
9        }
10    }
11
12    public static int divide(int num1, int num2) {
13        return num1 / num2;
14    }
15}

In this example, the divide method attempts to divide two numbers. If the second number is 0, an ArithmeticException is thrown. The try block is used to enclose the code that might throw the exception, and the catch block catches the exception and handles it by printing an error message.

The finally Block

The finally block is used to specify a block of code that will be executed whether an exception is thrown or not. This block is typically used to release resources or perform cleanup operations.

TEXT/X-JAVA
1public class FinallyBlockExample {
2
3    public static void main(String[] args) {
4        try {
5            int result = divide(10, 0);
6            System.out.println("Result: " + result);
7        } catch (ArithmeticException e) {
8            System.out.println("Error: Division by zero");
9        } finally {
10            System.out.println("Finally block executed");
11        }
12    }
13
14    public static int divide(int num1, int num2) {
15        return num1 / num2;
16    }
17}

In this example, the finally block is used to print a message regardless of whether an exception is thrown or not.

Custom Exception Classes

In addition to using the built-in exception classes, you can also create your own custom exception classes. Custom exceptions can be useful when you want to handle specific types of errors in a unique way.

Here's an example of a custom exception class:

TEXT/X-JAVA
1public class InsufficientBalanceException extends Exception {
2
3    public InsufficientBalanceException(String message) {
4        super(message);
5    }
6}
JAVA
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment