Sobes.tech
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:

  1. Checked Exceptions

    • Inherit from the Exception class, but not from RuntimeException.
    • Require mandatory handling (try-catch) or declaration in the method signature (throws).
    • Example: IOException, SQLException.
  2. Unchecked Exceptions

    • Inherit from RuntimeException.
    • Do not require mandatory handling or declaration.
    • Usually related to programming errors, for example, NullPointerException, IllegalArgumentException.
  3. Errors

    • Inherit from the Error class.
    • Represent serious problems that are usually not handled by the application, for example, OutOfMemoryError, StackOverflowError.

Example of handling a checked exception:

try {
    FileReader file = new FileReader("file.txt");
} catch (IOException e) {
    e.printStackTrace();
}