Java exception handling

Question

Answers ( 1 )

    0
    2024-01-25T21:36:47+00:00

    It is about handling exceptions in Java, which is a common task in Java programming to ensure robust and error-free code.

    In Java, exceptions are events that disrupt the normal flow of the program's instructions. Exception handling is a powerful mechanism that handles runtime errors to maintain the normal flow of the application. Java provides a robust and object-oriented way to handle exception scenarios, known as Java exception handling.

    Here are the key concepts and code examples related to Java exception handling:

    1. Try-Catch Block: The most common method of handling exceptions is by using a try-catch block. You enclose the code that might throw an exception within a try block, and the code to handle the exception in the catch block.

      try {
          // code that may throw an exception
          int data = 100/0; // this line throws an ArithmeticException
      } catch (ArithmeticException e) {
          System.out.println(e.getMessage());
      }
      
    2. Multiple Catch Blocks: A try block can be followed by multiple catch blocks for different types of exceptions.

      try {
          // code that may throw multiple exceptions
          int a[] = new int[5];
          a[5] = 30 / 0;
      } catch (ArithmeticException e) {
          System.out.println("Arithmetic Exception occurs");
      } catch (ArrayIndexOutOfBoundsException e) {
          System.out.println("ArrayIndexOutOfBounds Exception occurs");
      }
      
    3. Finally Block: The finally block always executes whether an exception is handled or not. It is used for cleanup code like closing a file or releasing resources.

      try {
          // code that may throw an exception
      } catch (Exception e) {
          // handling exception
      } finally {
          // cleanup code, like closing resources
      }
      
    4. Throwing an Exception: You can throw an exception using the throw keyword. This is mostly used for custom exceptions.

      void checkAge(int age) {
          if (age < 18) {
              throw new ArithmeticException("Not Eligible for voting");
          } else {
              System.out.println("Eligible for voting");
          }
      }
      
    5. Custom Exceptions: You can create your own exceptions by extending the Exception class.

      class MyException extends Exception {
          public MyException(String s) {
              super(s);
          }
      }
      
    6. Checked and Unchecked Exceptions: In Java, exceptions under Throwable are divided into two main categories: checked exceptions (direct subclasses of Exception except for RuntimeException and its subclasses) and unchecked exceptions (RuntimeException and its subclasses).

    7. Try-with-Resources: This feature introduced in Java 7 allows for automatic resource management. Resources like files or sockets declared within the try block are automatically closed after use.

      try(FileReader fr = new FileReader("file.txt")) {
          // read the file
      } catch (IOException e) {
          // handle IOException
      }
      
    8. Exception Propagation: An exception can be propagated (passed along) up the call stack.

      void a() {
          int data = 50/0;
      }
      
      void b() {
          a();
      }
      
      void c() {
          try {
              b();
          } catch(Exception e) {
              System.out.println("Exception handled");
          }
      }
      

    These are the primary aspects and methods of handling exceptions in Java. Effective exception handling is crucial for creating robust and fault-tolerant applications.

Leave an answer