views:

510

answers:

9

I'm trying to learn more about basic Java and the different types of Throwables, can someone let me know the differences between Exceptions and Errors?

+19  A: 

Errors should not be caught or handled (except in the rarest of cases). Exceptions are the bread and butter of exception handling. 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 conditions.

Look at a few of the subclasses of Error, taking some of their JavaDoc comments:

  • AnnotationFormatError - Thrown when the annotation parser attempts to read an annotation from a class file and determines that the annotation is malformed.
  • AssertionError - Thrown to indicate that an assertion has failed.
  • LinkageError - Subclasses of LinkageError indicate that a class has some dependency on another class; however, the latter class has incompatibly changed after the compilation of the former class.
  • VirtualMachineError - Thrown to indicate that the Java Virtual Machine is broken or has run out of resources necessary for it to continue operating.

There are really three important subcategories of Throwable:

  • Error - Something severe enough has gone wrong the most applications should crash rather than try to handle the problem,
  • Unchecked Exception (aka 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.
  • Checked Exception (aka Everything else) - Applications are expected to be able to catch and meaningfully do something with the rest, such as FileNotFoundException and TimeoutException...
Eddie
You should probably indicate your categories as Error, Unchecked (RuntimeException or subclass), Checked (Exception or subclass)
Robin
@Robin: Excellent point. I made the edit.
Eddie
A: 

Exceptions are a way to brake out of the normal flow of code when an error occurs. Generally exceptions are specified for an expected type of error.

An error is simply a piece of code that does not result in an acceptable value.

Tom Hubbard
He's probably talking about java.lang.Error
egaga
+3  A: 

Errors tend to signal the end of your application as you know it. It typically cannot be recovered from and should cause your VM to exit. Catching them should not be done except to possibly log or display and appropriate message before exiting.

Example: OutOfMemoryError - Not much you can do as your program can no longer run.

Exceptions are often recoverable and even when not, they generally just mean an attempted operation failed, but your program can still carry on.

Example: IllegalArgumentException - Passed invalid data to a method so that method call failed, but it does not affect future operations.

These are simplistic examples, and there is another wealth of information on just Exceptions alone.

Robin
+4  A: 

Sun puts it best:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.

R. Bemrose
+1  A: 

Two types of errors: Runtime and compile time. Errors are irrecoverable exceptions. In the case of compile time error the java compiler (javac) will fail, and compilation will end. In the case of a runtime error, a program will terminate execution. An exception, on the other hand, is a negative situation (error) handled by code, but not desired. This "throws an error" which can be handled by the programer in the "catch" statement.

An example is writing to a disk. Sometimes the program is unable to properly access the disk, permissions, deadlock, ect. This is a common error, so an exception is thrown and handled by java, which the programmer can then handle and respond to without crashing his program.

windfinder
+1  A: 

The description of the Error class is quite clear:

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 conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.

A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.

Cited from Java's own documentation of the class Error.

In short, you should not catch Errors, except you have a good reason to do so. (For example to prevent your implementation of web server to crash if a servlet runs out of memory or something like that.)

An Exception, on the other hand, is just a normal exception as in any other modern language. You will find a detailed description in the Java API documentation or any online or offline resource.

Tobias Müller
A: 

Here's a pretty good summary from Java API what an Error and Exception represents:

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 conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.

A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.

OTOH, for Exceptions, Java API says:

The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.

egaga
A: 

IMO an error is something that can cause your application to fail and should not be handled. An exception is something that can cause unpredictable results, but can be recovered from.

Example:

If a program has run out of memory it is an error as the application cannot continue. However, if a program accepts an incorrect input type it is an exception as the program can handle it and redirect to receive the correct input type.

Mr. Will
A: 

Another interesting question in my opinion is "Are Errors checked exceptions?"

No - you would never declare in the method signature "this method has a disastrous Error intentionally coded into it." It would be like forcing everyone to check for "what if Bob's an idiot today?" when they use my code.
Bob Cross