Exception Handling in java

Exception handling in Java is the process of dealing with runtime errors or exceptional situations that may occur during the execution of a program. The primary mechanisms for exception handling in Java are the try, catch, finally, and throw keywords.

Here’s a basic overview of how exception handling works in Java:

  1. try Block:
  • The try block encloses a section of code where an exception might occur.
  • It is followed by one or more catch blocks.
   try {
       // code that may throw an exception
   } catch (ExceptionType1 e1) {
       // handle ExceptionType1
   } catch (ExceptionType2 e2) {
       // handle ExceptionType2
   } finally {
       // code that will be executed regardless of whether an exception occurred or not
   }
  1. catch Blocks:
  • If an exception occurs in the try block, the corresponding catch block is executed.
  • You can have multiple catch blocks to handle different types of exceptions.
   try {
       // code that may throw an exception
   } catch (ExceptionType1 e1) {
       // handle ExceptionType1
   } catch (ExceptionType2 e2) {
       // handle ExceptionType2
   }
  1. finally Block:
  • The finally block contains code that will be executed regardless of whether an exception occurred or not.
  • It is often used for cleanup tasks, such as closing resources.
   try {
       // code that may throw an exception
   } catch (ExceptionType e) {
       // handle the exception
   } finally {
       // code that will be executed regardless of whether an exception occurred or not
   }
  1. throw Statement:
  • The throw statement is used to explicitly throw an exception.
  • It is often used in conjunction with the try block to handle exceptional situations.
   if (someCondition) {
       throw new SomeException("This is an example exception");
   }

Here’s a simple example:

import java.util.Scanner;

public class ExceptionHandlingExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        try {
            System.out.print("Enter a number: ");
            int num = scanner.nextInt();
            int result = 10 / num;
            System.out.println("Result: " + result);
        } catch (ArithmeticException e) {
            System.err.println("Error: " + e.getMessage());
        } finally {
            // Close the scanner in the finally block to ensure it is always executed
            scanner.close();
        }
    }
}

In this example, the program attempts to perform a division operation, and if the user enters 0, it will throw an ArithmeticException. The exception is caught in the catch block, and the finally block ensures that the Scanner is closed, regardless of whether an exception occurred or not.

A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of code. When an exceptional condition arises, an object representing that exception is created and thrown in the method that caused the error

Java exception handling is managed via five keywords: try, catch, throw, throws, and finally

catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2
}
// ...
finally {
// block of code to be executed after try block ends
}

Leave a Comment