Sobes.tech
Junior — Middle

What is the difference between checked and unchecked exceptions in Java?

sobes.tech AI

Answer from AI

In Java, exceptions are divided into checked and unchecked.

  • Checked exceptions are exceptions that are checked by the compiler at compile time. If a method can throw such an exception, it must be declared in its signature with throws, and the calling code must either handle this exception in a try-catch block or also declare it with throws. Examples: IOException, SQLException.

  • Unchecked exceptions are exceptions that are not checked by the compiler. They inherit from RuntimeException and can occur during runtime, for example, NullPointerException, ArrayIndexOutOfBoundsException. Handling them is not mandatory, and they usually indicate errors in the program logic.

Thus, the main difference is that checked exceptions require mandatory handling or declaration, while unchecked exceptions do not.

What is the difference between checked and unchecked… - sobes.tech