Try-Catch Blocks

Java handles runtime errors using try-catch blocks.

try {
  int result = 10 / 0;
} catch (ArithmeticException e) {
  System.out.println("Error: " + e.getMessage());
}

The code inside try is monitored for errors. If an exception occurs, the catch block handles it gracefully.

← PrevNext →