Junior — Middle
What system behavior is expected when an exception that needs to be checked is not handled?
sobes.tech AI
Answer from AI
In Java, there are two types of exceptions: checked and unchecked. Checked exceptions require mandatory handling (try-catch) or declaration in the method signature (throws).
If a checked exception is not handled or declared, the code simply will not compile — the compiler will generate an error.
If an unchecked exception (for example, RuntimeException) occurs at runtime and is not handled, it is "thrown" up the call stack. If the exception is not caught in the end, then:
- The current thread is interrupted and terminates.
- If it is the main thread, the program may terminate with an error.
- In some environments (for example, web servers), this may lead to error logging and continuation of other threads.
Thus, the absence of handling for checked exceptions is not allowed at compile time, and for unchecked exceptions — it leads to the abrupt termination of the thread when the exception occurs.