Junior — Middle
Can you list exceptions in Java that do not require mandatory handling or checking?
sobes.tech AI
Answer from AI
In Java, exceptions are divided into checked and unchecked. Exceptions that do not require mandatory handling (via try-catch) or declaration in the method signature (throws) are considered unchecked exceptions.
They include:
-
Subclasses of
RuntimeException:NullPointerExceptionArrayIndexOutOfBoundsExceptionIllegalArgumentExceptionArithmeticExceptionClassCastException- and others
-
Subclasses of
Error(for example,OutOfMemoryError,StackOverflowError), which are usually not handled in the application.
Example:
public void example() {
int a = 5 / 0; // will throw ArithmeticException, but does not require try-catch
}