Checked vs Unchecked Exceptions

Type of exceptions in java are checked exceptions and unchecked exceptions. This classification is based on compile-time checking of exceptions. There is also a classification based on runtime in java. It is not widely known! That is synchronous and asynchronous exceptions. First let us see the java checked exceptions and unchecked exceptions.

Checked Exceptions Vs Unchecked Exceptions in Java

At compile time, the java compiler checks that a program contains handlers for checked exceptions. Java compiler analyzes by which checked exceptions can result from execution of a method or constructor.For each checked exception which is a possible result, the throws clause for the method or constructor must mention the class or its superclasses of that exception.

The class RuntimeException and its subclasses, and the class Error and its subclasses are unchecked exceptions classes. Because the compiler doesn’t forces them to be declared in the throws clause. All the other exception classes that are part of Throwable hierarchy are checked exceptions.

Now let us see a see small discussion on why exceptions are classified as checked exceptions and unchecked exceptions.

Those unchecked exception classes which are the error classes (Error and its subclasses) are exempted from compile-time checking in java because they can occur at many points in the program and recovery from them is difficult or impossible.

Example: OutOfMemoryError

Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.

The runtime exception classes (RuntimeException and its subclasses) are exempted from compile-time checking because, in the judgment of the designers of the Java, having to declare such exceptions would not aid significantly in establishing the correctness of programs.

Example: NullPointerException

Thrown when an application attempts to use null in a case where an object is required.

So the java compiler doesn’t forces them to be declared in the above two cases.

Synchronous and Asynchronous Exceptions in Java

The interpreter executes the java program sequentially. An exception E can occur relative to a line (L) of program. That is that exception E will always occur at the execution of that line L. This is called Synchronous exception.

An asynchronous exception in java can occur at any point in the execution of a program.

hi,
I just want to know one thing in exception handling mechanism. FileNotFound is a checked exception.It means we are handling at compile time.But it is caught at run time when program executes. How a JVM will decide which none is Checked exception and Which one is Unchecked exception.

Can you explain the whole scenario of Exception handling ?
Thanks
Sandy

Checked Exceptions are exceptions that are handled by the compliler compile time.But it is caught at run time when program executes. How a JVM will decide which one is Checked exception and Which one is Unchecked exception.

checked exception are those exception which are declare as already it simple mean if we compile our program then it shows a specified exception …..eg.
import static java.lang.System.*;
import static javax.lang.Timer .*;
using this packade we get the checked exceptipon error .it give CE error

Checked exceptions are actually incredibly convenient. It kills me when I see other programmers catch the superclass and do something generic when they could do something specific to handle the various exceptions they might get.

I also am discouraged by .NET code that has try/catch littered at various parts in a huge block of code that more or less act as continue statements in a foreach.

Java can be a slow and painful beast, but they did at least make it easy to put some love into your exception handling. Much encouraged, and very appreciated to your maintainers when you do!

[…] Like NullPointerException, one exception that is very popular is ClassNotFoundException. At least in your beginner stage you might have got umpteen number of ClassNotFoundException. Java class loader is the culprit that is causing this exception. […]

I am not happy with the diff given between checked ad unchecked exceptions.You should give some examples abt the same and explain.
I m a c language & faculty following yr blog cont. to learn java. Please do the needful…

sir,
i have a question…sir we know that jvm throw an object on our programe of exception that is expose in our programe..
But in checked exception the compiler declare exception without the help of jvm..how..??

i want to know about interfaces….why we really need it?
means my question is in interfaces we can only define method declaration after that we have to again define in the class for their defination.
we can do directly this thing why two times writing ?

[…] and its subclassess are shown in dark green. RuntimeException and all its sub classes are called un-checked exceptions, since they need not be declared using throws in method signature and all other java exceptions are […]

See methods invoked directly without a catch block can throw an exception. Failure to catch and handle such exceptions results in the abrupt termination of the entire try block.
Doing such practice may causes any exception thrown in the try block to be lost, preventing any possible recovery method from handling that specific problem.

Though RuntimeException extends Exception class, how RuntimeException got this special behaviour(Un checked). I checked both classes. But, there is no difference. RuntimeException constructors simply calls super().
Who is handling this checked and unchecked behaviour. Will it be handling out of these two classes?
Does JVM itself checks whether it is subclass of RuntimeException or Exception?