As we know if any error or any unchecked exception occurs then our program will halt, then what are the differences between those?
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.
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.
Unchecked Exception:
RuntimeException
are known as unchecked exceptionsArithmeticException
,NullPointerException
, ArrayIndexOutOfBoundsException
etcArithmeticEceeption
. 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 techniquesError:
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
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
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.
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.