Differences between Exception and Error in java




In this post we will be discussing differences between java.lang.Exception and java.lang.Error



Differences between Exception and Error in java >




Property
1
serious problem?
Exception does not indicate any serious problem.
Error indicates some serious problems that our application should not try to catch.
2
divided into
Exception are divided into checked and unchecked exceptions.
Error are not divided further into such classifications.
3
Which classes are which type of exception? either
checked or unchecked exception?
The class Exception and all its subclasses that are not also subclasses of RuntimeException are checked exceptions.

The class RuntimeException and all its subclasses are unchecked exceptions.
Likewise,
The class Error and all its subclasses are unchecked exceptions.
Error and its subclasses are regarded as unchecked exceptions
4
Most frequently faced exception and errors
checked exceptions>
SQLException,
IOException,
ClassNotFoundException

unchecked exceptions>
NullPointerException, ArithmeticException,
VirtualMachineError, IOError, AssertionError, ThreadDeath,
OutOfMemoryError, StackOverflowError.
5
Why to catch or not to catch?
Application must catch the Exception because they does not cause any major threat to application.
Application must not catch the Error because they does cause any major threat to application.
Example >
Let’s say errors like OutOfMemoryError and StackOverflowError occur and are caught then JVM might not be able to free up memory for rest of application to execute, so it will be better if application don’t catch these errors and is allowed to terminate.



/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */


RELATED LINKS>

eEdit
Must read for you :