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.
Handles abnormal conditions that occur during program execution.
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!");
}
}
}
Output
Error: Division by 0!
Top comments (0)