Monday 6 August 2012

Checked vs Unchecked Exceptions in Java

Type of exceptions in java are checked exceptions and unchecked exceptions. This classification is based on compile-time checking of exceptions.

At compile time, the java compiler checks that a program contains handlers for checked exceptions.Either you have to enclose the code with a try-catch block or the method must declares that a method can throw a given exception using 'throws' clause, and the caller to that method must explicitly handle the exception (or 'throw it up' to the next caller up the chain).

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.

Unchecked exceptions behave as follows:



  • They they don't have to be explicitly caught. When an unchecked exception occurs, such as a NullPointerException, ClassCastException,OutOfMemoryError etc, Java will "handle" the exception automatically (see below).

  • Methods and constructors don't have to explicitly state that they can throw an unchecked exception. It's taken for granted that any method can throw them.

  • Indeed, certain Java bytecode instructions (such as array access, invoking a method on an object, integer division etc) can actually throw an unchecked exception.


Unchecked exceptions Example:


When an unchecked exception occurs, what you will see in practice in a simple single-threaded program is that Java prints out the stack trace of the exception and then terminates the program. For example, if we run the following program:



public class UnckedExceptionExample {

static void divide(int x, int y) {

System.out.println("x/y is:" + x / y);
}

public static void main(String[] args) {

divide(10, 0);
}
}


we'll get output such as the following:



Exception in thread "main" java.lang.ArithmeticException: / by zero
at UnckedExceptionExample.divide(UnckedExceptionExample.java:4)
at UnckedExceptionExample.main(UnckedExceptionExample.java:8)


Notice that in the above example,the divide method throws ArithmeticException if the parameter 'y' is zero.But we are not handling the exception or explicitly mentioning using throws clause.The reason for is that ArithmeticException is an unchecked run-time exception.

Checked exceptions Example:


When an checked exception occurs,either you have to handle it using a try-catch block or using the 'throws' clause mention that method may throw a checked exception.For example:

void writeFile() {
PrintWriter out = null;
try {
FileWriter fr = new FileWriter("C:\t1.txt");
out = new PrintWriter(fr);
out.println("finally block example");
} catch (IOException e) {
System.out.println(e);
}
}

The writeFile method may throw a checked IOException, so enclosed the code with a try-catch block to handle it.Another option that you have is declare that a method can throw a given exception using 'throws' clause, and the caller to that method must explicitly handle the exception (or 'throw it up' to the next caller up the chain).


void writeFile() throws IOException {
PrintWriter out = null;
FileWriter fr = new FileWriter("C:\t1.txt");
out = new PrintWriter(fr);
out.println("finally block example");

}


No comments:

Post a Comment