DEV Community

Mohamed Ajmal
Mohamed Ajmal

Posted on

Exception Handling in Java.

Exception Handling in Java is a mechanism used to handle both compile-time (checked) and runtime (unchecked) exceptions, allowing a program to continue execution smoothly even in the presence of errors.

  1. Handles abnormal conditions that occur during program execution.

  2. Helps maintain program stability by preventing unexpected termination.

class ExceptionDemo{
public static void main(String[] args) {

    int i = 10;
    int j = 0;

    try {
        int ans = i / j;
        System.out.println("Answer: " + ans);
    } catch (ArithmeticException e){
        System.out.println("Error: Division by 0!");
    } 
}
Enter fullscreen mode Exit fullscreen mode

}

Output

Error: Division by 0!

Top comments (0)