Checked and Unchecked Exception in Java
Checked and Unchecked Exception in Java
And, if there is no code to handle them, then the compiler checks whether
the method is declared using the throws keyword. And, if the compiler finds
neither of the two cases, then it gives a compilation error. A checked
exception extends the Exception class.
package com.techvidvan.exceptions;
import java.io.File;
import java.io.FileReader;
public class CheckedExceptions
{
public static void main(String args[])
{
File file = new File("/home/techvidvan/file.txt");
FileReader fileReader = new FileReader(file);
System.out.println("Successful");
}
}
Output:
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Unhandled exception type FileNotFoundException
at
project1/com.techvidvan.exceptions.CheckedExceptions.main(CheckedExceptions.java:1
0)
Now, if we use the throws keyword with the main then we will not get any
error:
package com.techvidvan.exceptions;
import java.io.*;
public class CheckedExceptions
{
public static void main(String args[]) throws IOException
{
File file = new File("/home/techvidvan/file.txt");
FileReader fileReader = new FileReader(file);
System.out.println("Successful");
}
}
Output:
Successful
import java.io.*;
public class Example
{
public static void main(String args[])
{
FileInputStream fis = null;
try
{
fis = new FileInputStream("/home/techvidvan/file.txt");
}catch(FileNotFoundException fnfe)
{
System.out.println("The specified file is not " +
"present at the given path");
}
int k;
try
{
while(( k = fis.read() ) != -1)
{
System.out.print((char)k);
}
fis.close();
}catch(IOException ioe)
{
System.out.println("I/O error occurred: "+ioe);
}
}
}
Output:
The specified file is not present at the given path
Exception in thread “main” java.lang.NullPointerException
at project1/com.techvidvan.exceptions.Example.main(Example.java:15)
package com.techvidvan.exceptions;
public class UnCheckedExceptions
{
public static void main(String args[])
{
//ArrayIndexOutOfBoundsException
int array[] = {1, 2, 3, 4, 5};
System.out.println(array[7]);
}
}
Output:
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: Index 7 out
of bounds for length 5
at
project1/com.techvidvan.exceptions.UnCheckedExceptions.main(UnCheckedExceptions.ja
va:8)
Note: Though the compiler does not check these exceptions, it doesn’t mean that
we shouldn’t handle them. In fact, we should handle them more carefully to
provide a safe exit.
Comparison between Checked Exceptions and Java
Unchecked Exception in Java
S.N. Checked Exception Unchecked Exception
3 The compiler checks a checked exception. The compiler ignores the unchecked exceptions.
The compiler will give an error if the code does The compiler will never give an error if the code does
5
not handle the checked exceptions. not handle the unchecked exceptions.
JVM needs to catch and handle the checked JVM needs not catch and handle the unchecked
7
exception. exception.
Checked exceptions are predictable erroneous Unchecked exceptions are erroneous situations
8 situations that might reasonably occur during
the proper use of an API. that mainly occur due to mistakes in programming logic.
We can create user-defined checked exceptions We can create user-defined Unchecked exceptions
9
by extending java.lang.Exception class. by extending RuntimeException class.
FileNotFoundException ArrayIndexOutOfBoundsException
NoSuchFieldException NumberFormatException
SQLException InputMismatchException
10
IOException IllegalStateException
InterruptedException ArithmeticException
NoSuchMethodException NullPointerException
ClassNotFoundException SecurityException
Exception Hierarchy
Java Exceptions are broadly divided into two parts: checked exceptions and
unchecked exceptions. The following figure shows this Exception hierarchy
in Java.
if (!isCorrectFileName(fileName))
{
throw new IncorrectFileNameException("File name is invalid: " + fileName );
}
If the input file name is an empty string, it means there is an error in the
code. In this case, we should throw an unchecked exception.
Summary
Exceptions in Java may hinder the normal execution of a program. There are
two kinds of exceptions in Java which are Checked or compile-time
exceptions and Unchecked or runtime exceptions. In checked exceptions, we
have to write the code to handle them otherwise the compiler gives an error.
And, in runtime exceptions, it is not necessary to write the code to handle the
exception and still, the compiler does not give any error. This article gave you
the detailed points of differences between both these exceptions in Java. You
might have understood when to use either of two exceptions.