Error and Exception

The Javadoc explains it well:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal condition.
Example: OutOfMemoryError - Not much you can do as your program can no longer run.

The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.
Example: IllegalArgumentException - Passed invalid data to a method so that method call failed, but it does not affect future operations.

In Single line : You can make your application recover from an Exception but not from Error.

There are really three important subcategories of Throwable:

  1. Error - Something severe enough has gone wrong the most applications should crash rather than try to handle the problem,
  2. Unchecked Exception (RuntimeException) - Very often a programming error such as a NullPointerException or an illegal argument. Applications can sometimes handle or recover from this Throwable category -- or at least catch it at the Thread's run() method, log the complaint, and continue running.
  3. Checked Exception (Everything else) - Applications are expected to be able to catch and meaningfully do something with the rest, such as FileNotFoundException and TimeoutException

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...