Junior — Middle
Can you list the main categories of exceptions in Java?
sobes.tech AI
Answer from AI
In Java, the main categories of exceptions are:
-
Checked Exceptions
- Inherit from the
Exceptionclass, but not fromRuntimeException. - Require mandatory handling (try-catch) or declaration in the method signature (
throws). - Example:
IOException,SQLException.
- Inherit from the
-
Unchecked Exceptions
- Inherit from
RuntimeException. - Do not require mandatory handling or declaration.
- Usually related to programming errors, for example,
NullPointerException,IllegalArgumentException.
- Inherit from
-
Errors
- Inherit from the
Errorclass. - Represent serious problems that are usually not handled by the application, for example,
OutOfMemoryError,StackOverflowError.
- Inherit from the
Example of handling a checked exception:
try {
FileReader file = new FileReader("file.txt");
} catch (IOException e) {
e.printStackTrace();
}