Exception Handling in Java
Exception = an error that happens during program execution
Exception Handling = a way to detect, handle, and prevent program crash
try → code that may cause error
catch → handles the error
try–catch Example
public class Example {
public static void main(String[] args) {
try {
int x = 10 / 0; // Error: division by zero
}
catch (ArithmeticException e) {
[Link]("Cannot divide by zero!");
}
}
}