Sobes.tech
Junior — Middle

Kuidas rakendada Java keeles veadekäsitsemise mehhanismi?

sobes.tech AI

Vastus AI-lt

In Java, the error handling mechanism is implemented using exceptions. Exceptions allow separating the normal execution flow from error handling.

Main keywords:

  • try — block where an exception may occur.
  • catch — block that catches and handles the exception.
  • finally — block that always executes, regardless of whether an exception occurred or not.
  • throw — operator to generate an exception.

Example:

try {
    int result = 10 / 0; // will throw ArithmeticException
} catch (ArithmeticException e) {
    System.out.println("Division by zero is not possible");
} finally {
    System.out.println("Finally block executed");
}