Exception Handling

try, catch, and throw statements are used for handling exceptions, which are unexpected events or errors that can occur during the execution of a program.

  1. try statement: The try block is used to enclose a section of code where you expect an exception might occur. The syntax for a try block is as follows:
try {
    // Code that may cause an exception
} 
  1. catch statement: The catch block is used to handle exceptions that are thrown within the try block. It comes after the try block and specifies the type of exception it can handle. The syntax for a catch block is as follows:
catch (ExceptionType e) {
    // Exception handling code
}
  1. throw statement: The throw statement is used to manually throw an exception. The syntax for the throw statement is as follows:
throw new ExceptionType("Exception message");

Example

public class ExceptionExample {
    public static void main(String[] args) {
        try {
            int result = divide(10, 0);
        } catch (ArithmeticException e) {
            System.out.println("An arithmetic exception occurred: " + e.getMessage());
        }
    }

    public static int divide(int a, int b) {
        if (b == 0) {
            throw new ArithmeticException("Division by zero is not allowed.");
        }
        return a / b;
    }
}