Lorem ipsum dolor sit amet, consectetur adipiscing elit. Test link

Exception Handling

A. Exception Types with explanation with example in Java:

  1. Checked Exception: Checked exceptions are the exceptions that are checked by the compiler during compilation time. They occur when the code is executed and can be caught and handled. Examples include FileNotFoundException, IOException, ClassNotFoundException, SQLException, etc.
  2. Unchecked Exception: Unchecked exceptions are the exceptions that are not checked by the compiler during compilation time. They occur during runtime and can be caught and handled. Examples include ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, etc.
  3. Error: Errors are the serious problems that occur during the execution of a program, which cannot be caught and handled. Examples include OutOfMemoryError, StackOverflowError, etc.

B. try-catch-finally block with explanation with example in Java:

The try-catch-finally block is used to handle exceptions in Java. The try block contains the code that is being monitored for exceptions, and the catch block is used to catch and handle the exceptions. The finally block is used to execute code that needs to be executed regardless of whether an exception occurs or not.

Example:

public class TryCatchFinallyExample {
  public static void main(String[] args) {
    try {
      int a = 5/0;
    } catch (ArithmeticException e) {
      System.out.println("Cannot divide by zero");
    } finally {
      System.out.println("This will always execute");
    }
  }
}

In this example, we are trying to divide 5 by 0, which is not possible and will result in an ArithmeticException. We catch the exception in the catch block and print an error message. The finally block will always execute regardless of whether an exception occurs or not.

C. Exception Propagation with explanation with example in Java:

Exception propagation refers to the mechanism of passing an exception object from one method to another. When an exception occurs in a method, the JVM looks for a catch block that can handle the exception. If it cannot find a catch block in the method, it moves to the calling method to look for a catch block. This process continues until a catch block is found or the exception reaches the top of the call stack.

Example:

public class ExceptionPropagationExample {
  public static void main(String[] args) {
    try {
      method1();
    } catch (Exception e) {
      System.out.println("Exception caught: " + e);
    }
  }

  public static void method1() throws Exception {
    method2();
  }

  public static void method2() throws Exception {
    method3();
  }

  public static void method3() throws Exception {
    throw new Exception("Exception occurred in method3");
  }
}

In this example, the method3 throws an exception that is propagated to the method2. Since method2 does not have a catch block, the exception is propagated to method1. The catch block in method1 catches the exception and prints an error message.

D. Checked and Unchecked Exceptions with explanation with example in Java:

Checked exceptions are the exceptions that are checked by the compiler during compilation time. They occur when the code is executed and can be caught and handled. Examples include FileNotFoundException, IOException, ClassNotFoundException, SQLException, etc.

Example:

import java.io.*;
public class CheckedExceptionExample {
  public static void main(String[] args) {
    try {
      BufferedReader br = new BufferedReader(new FileReader("file.txt"));
      String line = br.readLine();
      System.out.println(line);
    } catch (IOException e) {
      System.out.println("IOException occurred: " + e);
    }
  }
}

In this example, we are trying to read data from a file using BufferedReader. The FileReader constructor throws a FileNotFoundException, which is a checked exception

Unchecked exceptions are the exceptions that are not checked by the compiler during compilation time. They occur during runtime and can be caught and handled. Examples include ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, etc.

Example:

public class UncheckedExceptionExample {
  public static void main(String[] args) {
    int a = 5, b = 0;
    try {
      int c = a/b;
    } catch (ArithmeticException e) {
      System.out.println("ArithmeticException occurred: " + e);
    }
  }
}

In this example, we are trying to divide 5 by 0, which is not possible and will result in an ArithmeticException, which is an unchecked exception. We catch the exception in the catch block and print an error message.

Post a Comment