Difference Between Exception and Error in Java?
Difference Between Exception and Error in Java?
Exception → Something went wrong in your code, but you can catch and fix it using try-catch.
try {
int a = 10 / 0; // Causes ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero!");
}
Error → Something is wrong with the system, like memory issues. You cannot fix it in your code.
// Example of an Error (not handled by try-catch)
public class ErrorExample {
public static void main(String[] args) {
main(args); // Causes StackOverflowError (infinite recursion)
}
}