Sunday, 16 April 2017

Checked and Unchecked Exception in java

Two categories of Exception that we come across are:

Checked Exception:

Checked Exception extends java.lang.Exception

If there is possibility that the code written may result in throwing exception then compiler senses it and reminds programmer to handle such codes in either of these two ways during compilation:
  • enclose code within try/catch
  • declare the exception using throws keyword
At the very basic level you can think of it as exceptions which are caught at the compile time.

For example,

You have a code where you want to read a file from particular location  say ("D:\\JavaFiles\\myFile.txt")


class MyFileReader {

    public static void main(String[] args) {

        FileReader file = new FileReader("D:\\JavaFiles\\myFile.txt");
        BufferedReader fileInput = new BufferedReader(file);
        // Print first 4 lines of file "C:\test\a.txt"
        for (int counter = 0; counter < 4; counter++)

What would be fate of above code?

It is sure to throw compile time error.

Reason being the presence of FileReader which throws Checked Exception FileNotFoundException and readLine & close which throws checked exception IOException.

In order save your code from compile time error enclose the code within try/catch block. Or declare all Exceptions that could possibly occur using throws like this,

 public static void main(String[] args) throws IOException

Since FileNotFoundException is the child (sub class) of IOException so declaring only IOException using throws will serve the purpose here.


Unchecked Exceptions:

Unchecked Exceptions extends java.lang.RuntimeException

Unchecked Exceptions are those that goes beyond the range of compilers and escapes their notice. It occurs due to error in programming logic. There are exceptions which can only be detected at run time such as illogical codes. 

Like, a code where you perform division by 0(zero) or you try to access value from an index which is not present in the array. 

These kind of code will compile fine and will look perfect until run. So unchecked Exception are run time exceptions.

For example consider this code,

class MyFile {

    public static void main(String[] args) {
        // Division by 0
        int x = 5/0; // illogical code will throw ArithmeticException

The above code will not show any compile time error. But if you run it, you will find ArithmeticException.

So as a programmer you are never expected to write a code like,
int i=5, int j=0;
int k=i/j;

because it is loop hole in coding logic and will show its erroneous face while you run it and will result in abrupt termination of your code if it is not taken care by try/catch block.
And if you write such code then you have to waste lot of time in debugging your code and find out this kind of logical error.


You may also like to read:

No comments:

Post a Comment

If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.