Sobes.tech
Junior

What types of exceptions exist in Java?

sobes.tech AI

Answer from AI

In Java, exceptions are divided into two main categories:

  1. Checked Exceptions — these are exceptions that the compiler requires to be handled or declared in the method with throws. They are usually related to external resources, such as files or networks. Examples: IOException, SQLException.

  2. Unchecked Exceptions — these are runtime exceptions that do not require mandatory handling or declaration. They inherit from RuntimeException. Examples: NullPointerException, IllegalArgumentException.

There are also Errors — serious problems that are usually not handled by the application, such as OutOfMemoryError.

Example of exception handling:

try {
    // code that may throw an exception
} catch (IOException e) {
    e.printStackTrace();
}