Two categories of Exception that we come across are:
Checked 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")import java.io.*;
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++) System.out.println(fileInput.readLine()); fileInput.close(); }}
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.
Examples: SQLException, ClassNotFoundException, IOException, etc
Unchecked Exceptions:
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 System.out.println(x); }}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.
Examples: ArithmeticException, ArrayIndexOutOfBoundsException,NullPointerExceptions, etc
You may also like to read:
- How to create custom Exception?
- arraylist vs linkedlist
- arraylist vs hashset
- iterator vs enumeration
- splitting a string at delimiter in java
- String is immutable in java
No comments:
Post a Comment