Junior — Middle
Explain what raw exceptions in programming language are.
sobes.tech AI
Answer from AI
In Java, unchecked exceptions are exceptions that do not require mandatory catching or declaring in the method signature with throws. They inherit from the RuntimeException class.
Main features of unchecked exceptions:
- The compiler does not force the programmer to handle or declare them.
- They usually arise from programming errors, such as
NullPointerException,ArrayIndexOutOfBoundsException. - They can be caught, but it is not mandatory.
Example:
public void divide(int a, int b) {
int result = a / b; // if b == 0, an ArithmeticException (unchecked) will occur
}
Here, ArithmeticException is an unchecked exception that can occur at runtime but does not require mandatory catching.