Junior — Middle
Can exceptions occur that are not explicitly checked during program execution?
sobes.tech AI
Answer from AI
Yes, Java has unchecked exceptions that do not require explicit declaration or handling in the code. These include, for example, RuntimeException and its subclasses (NullPointerException, IllegalArgumentException, etc.). Such exceptions can occur during program execution and do not necessarily need to be wrapped in try-catch blocks or declared in throws.
Checked exceptions, on the other hand, require either handling or declaration in the method signature.
Example of an unchecked exception:
public void divide(int a, int b) {
int result = a / b; // may throw ArithmeticException when dividing by zero
}
Here, ArithmeticException is an unchecked exception, and the compiler will not force explicit handling of it.