12

As we know if any error or any unchecked exception occurs then our program will halt, then what are the differences between those?

1
  • 1
    do you mean Error as in java.lang.Error ?
    – LB40
    Apr 22, 2010 at 18:21

7 Answers 7

16

In short:

You can, and probably should, recover from an exception.

You can, but should not, recover from an error.

3
  • 1
    @Eric Eijkelenboom: Maybe you meant "you should not recover from an error"? Because Error extends Throwable and any Throwable is surely easy to catch and a great many Error can be recovered from (OutOfMemoryError, just to name one, is quite easy to work around in applications that have big caches that can be discarded at will). Apr 22, 2010 at 19:48
  • Yes, I should have been more specific there, post edited. +1 for pointing this out :) Apr 23, 2010 at 6:31
  • short but meaningful.
    – Saif
    Apr 30, 2015 at 11:49
14

From the Error Javadoc:

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.

Versus the Exception Javadoc

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

So, even though an unchecked exception is not required to be caught, you may want to. An error, you don't want to catch.

6

Unchecked Exception:

  • The classes that extend RuntimeException are known as unchecked exceptions
  • Unchecked exceptions are not checked at compile-time rather they are checked at runtime.And thats why they are also called "Runtime Exception"
  • They are also programmatically recoverable problems but unlike checked exception they are caused by faults in code flow or configuration.
  • Example: ArithmeticException,NullPointerException, ArrayIndexOutOfBoundsException etc
  • Since they are programming error, they can be avoided by nicely/wisely coding. For example "dividing by zero" occurs ArithmeticEceeption. We can avoid them by a simple if condition - if(divisor!=0). Similarly we can avoid NullPointerException by simply checking the references - if(object!=null) or using even better techniques

Error:

  • Error refers irrecoverable situation that are not being handled by try/catch

  • Example: OutOfMemoryError, VirtualMachineError, AssertionError etc.

    This question may also be helpful in this context - Runtime/Checked/Unchecked/Error-Exception

2

From the JavaDoc:

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

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.

So, the only difference technically is that they are two different classes. You will only catch both if you declare

catch (Throwable e) { }

But there is a big difference in how they are intended to be used. Unchecked exceptions (RuntimeExceptions) are intended to deal with programming errors and other unexpected problems, but should be caught and handled in the application. Errors are intended to represent problems that the program cannot deal with, such as running out of memory.

1

Just as checked exceptions are useful for signaling when your methods cannot fulfill their contract, there are other errors outside of your control that can occur that prevent the Java virtual machine from fulfilling its specification, such as when memory is exhausted. Since you can't plan for such errors ahead of time, you would have to catch them everywhere, which defeats the principle of maintaining uncluttered code. Therefore, these errors are unchecked exceptions, meaning exceptions that you don't have to include in a throws clause. You are welcome to catch them (well, some of them), but the compiler won't make you do it. Unchecked exceptions fall into two categories: those that extend RuntimeException, and those that extend Error. I realize that I said earlier that classes inheriting from class Exception are checked exceptions, but that's only half true: the whole truth is that classes in the Exception hierarchy other than those in the RuntimeException sub-hierarchy are checked exceptions.

Exceptions that extend RuntimeException represent errors that you may want to handle, although you're not required to.

As I stated above, RuntimeExceptions are not checked because making you advertise them would have no effect on establishing the correctness of your methods, and would unnecessarily clutter your otherwise very readable code. Exceptions derived from the Error class, on the other hand, are unchecked because you never want to catch them! Error exceptions are severe errors that require shutting down the virtual machine. InternalError, which I used above, extends VirtualMachineError, which is an Error subclass. OutOfMemoryError is another obvious severe error, but there are others, like StackOverflowError, and various LinkageErrors. A linkage error means something has gone amiss when the class loader tried to load a class for execution, and commonly occurs either because some external source has introduced malicious code in an attempt to circumvent Java's security mechanism, or it came from an out-of-spec byte code generator.

1

Difference between errors and unchecked exceptions in java?

Unchecked exceptions = class RuntimeException and its subclasses + class Error and its subclasses

There fore the error are part of unchecked exception. Unchecked exception also contains RuntimeException and its subclasses.

1

The term «reasonable» is relative. As it is, also, the term «application»

As middleware developer, I'm used to deal with unchecked exceptions thrown, or simply ignored, by application developers

What is reasonable to be catched by an application is is a subset of what is reasonable to be catched by its underlying infrastructure (that is, itself, a kind of application)

That's where unchecked exceptions are different from errors. An unchecked can be catched by the infrastructure (i.e. to be registered in a.database) but ignored by an application

Registering an error could be impossible because there could be no JVM to run the registering code (or even the catching code), that's why it's not reasonable to catch them

As an aside, checked exceptions are IMHO overused. Catching them to promote to runtime exceptions is too much usual

1
  • "there could be no JVM to run the registering code (or even the catching code)" That's the key right there. If you have an Error, something really bad happened, and you shouldn't assume that the VM is structurally sound. With an Exception, checked out unchecked, things technically will still work.
    – ndm13
    Jun 13, 2017 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.