In Java , exceptions are events that disrupt the normal flow of the program’s execution. Exceptions are categorized into different types based on their origin and handling mechanisms.

1.Checked Exceptions

Checked exceptions are exceptions that are checked at compile-time by the compiler. They must be either handled using a try-catch block or declared using a throws keyword.

Definition:

Exceptions that are checked at compile time, indicating situations that a programmer should anticipate and recover from.

Example:

[pastacode lang=”java” manual=”import%20java.io.File%3B%0A%0Aimport%20java.io.FileReader%3B%0A%0A%0A%0A%0Apublic%20class%20CheckedExceptionExample%20%7B%0A%0A%C2%A0%C2%A0%C2%A0%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20try%20%7B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20File%20file%20%3D%20new%20File(%22test.txt%22)%3B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20FileReader%20fr%20%3D%20new%20FileReader(file)%3B%20%2F%2F%20FileNotFoundException%20may%20be%20thrown%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20%7D%20catch%20(Exception%20e)%20%7B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20e.printStackTrace()%3B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20%7D%0A%0A%C2%A0%C2%A0%C2%A0%20%7D%0A%0A%7D” message=”” highlight=”” provider=”manual”/]

Features:

  • Compiler enforces handling of checked exceptions.
  • Examples include IOException, SQLException, and ClassNotFoundException.

Advantages:

  • Forces developers to handle error conditions properly.
  • Improves code robustness by ensuring critical scenarios are managed.

Uses:

  • Typically used for recoverable errors like file I/O operations or network errors.

2.Unchecked Exceptions

Unchecked exceptions are exceptions that are not checked at compile time. They occur due to programming logic errors and are mostly runtime exceptions.

Definition:

Exceptions that are not checked during compilation but occur during the execution of the program.

Example:

[pastacode lang=”java” manual=”public%20class%20UncheckedExceptionExample%20%7B%0A%0A%C2%A0%C2%A0%C2%A0%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20int%20a%20%3D%205%3B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20int%20b%20%3D%200%3B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20int%20result%20%3D%20a%20%2F%20b%3B%20%2F%2F%20ArithmeticException%20will%20be%20thrown%0A%0A%C2%A0%C2%A0%C2%A0%20%7D%0A%0A%7D” message=”” highlight=”” provider=”manual”/]

Features:

  • Unchecked exceptions are subclasses of RuntimeException.
  • Examples include ArithmeticException, NullPointerException, and ArrayIndexOutOfBoundsException.

Advantages:

  • Simpler to write code since handling them is optional.
  • Focuses on improving logical consistency in code to avoid such exceptions.

Uses:

  • Often used to indicate programming bugs or logic errors that should be avoided in development.

3.Error

Errors are serious problems that are generally beyond the control of the application and cannot be handled using standard exception-handling techniques.

Definition:

Errors represent problems that are not meant to be caught or handled by normal programs. They often indicate issues with the environment in which the application is running.

Example:

[pastacode lang=”java” manual=”public%20class%20ErrorExample%20%7B%0A%0A%C2%A0%C2%A0%C2%A0%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20int%5B%5D%20arr%20%3D%20new%20int%5BInteger.MAX_VALUE%5D%3B%20%2F%2F%20OutOfMemoryError%20may%20be%20thrown%0A%0A%C2%A0%C2%A0%C2%A0%20%7D%0A%0A%7D” message=”” highlight=”” provider=”manual”/]

Features:

  • Errors are subclasses of java.lang.Error.
  • Examples include OutOfMemoryError and StackOverflowError.

Advantages:

  • Identifies critical issues that the application cannot recover from.
  • Helps in distinguishing between recoverable and non-recoverable problems.

Uses:

  • Used to represent problems related to the Java Virtual Machine (JVM), such as memory allocation issues or other severe runtime problems.