Principal
What are the types of exceptions in Java and how do they differ?
sobes.tech AI
Answer from AI
In Java, exceptions are divided into several types:
-
Checked exceptions
- Inherit from the
Exceptionclass, but not fromRuntimeException. - The compiler requires mandatory handling (try-catch) or declaration in the method signature (
throws). - Used for errors that can be foreseen and handled, such as
IOException,SQLException.
- Inherit from the
-
Unchecked exceptions
- Inherit from
RuntimeException. - The compiler does not require mandatory handling.
- Usually indicate programming errors, such as
NullPointerException,IllegalArgumentException.
- Inherit from
-
Errors
- Inherit from the
Errorclass. - Represent serious problems that are usually not handled by the application, such as
OutOfMemoryError,StackOverflowError.
- Inherit from the
Thus, the main difference is the need for handling and the nature of the error: checked exceptions require explicit handling, unchecked do not, and errors signal critical runtime issues.