• Networking
  • Programming
  • DBMS
  • Operating System
  • Internet
  • Hardware
  • Software

Tech Differences

Know the Technical Differences

Difference Between Checked and Unchecked Exception in Java

Checked-and-Unchecked-Exception3“Throwable” is the parent class of the classes Error and Exception. The class “RuntimeException” and its subclasses, the class “Error” and its child classes are the “Unchecked exceptions” whereas, the remaining subclasses of the class “Exception” except “RuntimeException” are the checked exceptions.

The basic difference between checked and unchecked exception is that the checked exceptions are checked by the compiler whereas, the compiler does not check the unchecked exceptions.

Let us discuss the other differences between checked and unchecked exceptions with the help of the comparison chart.

Content: Checked Vs Unchecked Exception

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison Chart

Basis for ComparisonChecked ExceptionUnchecked Exception
BasicThe compiler checks the checked exception.The compiler does not check the Unchecked exception.
Class of ExceptionExcept "RuntimeException" class all the child classes of the class "Exception", and the "Error" class and its child classes are Checked Exception."RuntimeException" class and its child classes, are"Unchecked Exceptions".
HandlingIf we do not handle the checked exception, then the compiler objects.Even if we do not handle the unchecked exception, the compiler doesn't object.
CompilationThe program doesn't compile if there is an unhandled checked exception in the program code.The program compiles successfully even if there is an unhandled unchecked exception in the program code.

Definition of Checked Exception

The checked exceptions are those exceptions which are checked by the compiler for the smooth execution of the program. The compiler checks whether the program can handle the checked exception or not. If the code is unable to handle the exception there occurs a compilation error. All the subclasses of class “Exception” except the RuntimeException class are the checked exceptions.

Let us study the checked exception with an example.

import java.io.*;
class Example {
public static void main(String args[])
{
FileInputStream fis = null;
fis = new FileInputStream("B:/myfile.txt"); //This constructor FileInputStream(File filename) throws FileNotFoundException which is a checked exception.
int k;
while(( k = fis.read() ) != -1) { //Method read() of FileInputStream class also throws a checked exception: IOException.
System.out.print((char)k);
}
fis.close(); //The method close() closes the file input stream it throws IOException.
}
}

In above code, we are trying to open, read and display file contents. It may happen that the file is not present, then FileNotFoundException will occur which is a checked exception.

If checked exception is not handled, then the compiler won’t allow the program to be compiled smoothly. It will show compile time error. So, for smooth compilation, the checked exception must be caught or declared to be thrown.

Definition of Unchecked Exception

Unchecked Exceptions are those exceptions which are not checked by the compiler. The compiler compiles the program successfully even if the exceptions are not handled by the code. The class “Error” and its child classes, the class “Runtime” and its subclasses are the unchecked exceptions.

Let us see an example of an unchecked exception.

class Example {
public static void main(String args[])
{
int arr[] ={1,2,3,4,5};
System.out.println(arr[7]); //ArrayIndexOutOfBoundsException.
}
}

In above code, you can see that there is an ArrayIndexOutOfBoundsException, as I am trying to access the element which does not exist. As this is an unchecked exception, compile time error will not occur, and the file will be compiled without any error. But the program won’t execute till the exception is handled. So, for smooth execution, the exceptions must be caught or declared to be thrown.

Key Differences Between Checked and Unchecked Exception

  1. Checked exceptions are in knowledge of compiler whereas, Unchecked exceptions are not in knowledge of compiler.
  2. Except RuntimeException and Error class all the classes are checked exception. On the other hand, RuntimeException and Error classes are unchecked exceptions.
  3. If the checked exceptions are not handled by the code then the compiler objects. On the other hand, if we do not handle unchecked exceptions in the code the compiler doesn’t object.
  4. If checked exceptions occur in the code the code will not compile whereas, even if unchecked exceptions are not handled by the code the compiler still compiles the code.

Note

Both checked or unchecked exception compulsorily occurs during runtime. They are only checked or unchecked by the compiler during compile time.

Conclusion

Both the checked and unchecked exceptions must be handled in order to execute the program smoothly.

Related Differences:

  1. Difference Between Error and Exception in Java
  2. Difference Between throw and throws in Java
  3. Difference Between Final, Finally and Finalize in Java
  4. Difference Between C++ and Java
  5. Difference Between Inheritance and Polymorphism

Comments

  1. Teja says

    December 28, 2018 at 5:00 am

    Can you please check once that Error comes under checked exception?

    Reply
  2. Neelam says

    November 19, 2020 at 5:20 pm

    Very well written, illustrated and explained.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Top 10 Differences

  • Difference Between OLTP and OLAP
  • Difference Between while and do-while Loop
  • Difference Between Guided and Unguided Media
  • Difference Between Preemptive and Non-Preemptive Scheduling in OS
  • Difference Between LAN, MAN and WAN
  • Difference Between if-else and switch
  • Difference Between dispose() and finalize() in C#
  • Difference Between for and while loop
  • Difference Between View and Materialized View
  • Difference Between Server-side Scripting and Client-side Scripting

Recent Addition

  • Difference Between Java and Python
  • Difference Between PHP and HTML
  • Difference Between GPS and GNSS 
  • Difference Between Virtualization and Containerization
  • Difference Between Storage and Memory

Categories

  • Artificial Intelligence
  • DBMS
  • Hardware
  • Internet
  • Networking
  • Operating System
  • Programming
  • Software

Copyright © 2025 · Tech Differences · Contact Us · About Us · Privacy