Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a Checked Exception?
2.1.
Example
3.
What is an Unchecked Exception?
3.1.
Example
4.
Differences between Checked and Unchecked Exceptions in Java
5.
Frequently Asked Questions
5.1.
What is the difference between a checked and unchecked exceptions?
5.2.
What is the difference between RuntimeException and exception in Java?
5.3.
What is checked and unchecked exception in C++?
5.4.
What is the type of unchecked exception?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Difference Between Checked and Unchecked Exception

Author Gunjan Batra
0 upvote
gp-icon
Data structures & algorithms (Beginner to Intermediate)
Free guided path
13 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

Java exception is an unexpected issue that arises while a program is running, causing it to deviate from its normal operation. These exceptions can disrupt the flow of the program and need to be handled properly to prevent crashes and ensure smooth execution. There are two types of exceptions: 

  1. Checked exceptions
  2. Unchecked exceptions
Difference Between Checked and Unchecked Exception

What is a Checked Exception?

  • A checked exception is the one that the compiler checks or notifies during compilation.
  • Checked Exceptions are also known as compile-time exceptions.
  • These exceptions cannot be ignored.
  • If a code within a function throws a checked exception, the function must handle the exception or specify it using the throws keyword.

Example

A FileReader class is used in a file handling programme to read data from a file. If the file specified in its constructor does not exist, a FileNotFoundException occurs, and the compiler prompts the programmer to handle the exception.

Here, If the Name.txt file is not present in the specified location during the compilation, this code will throw an error.

import java.io.File;
import java.io.FileReader;

public class Test{

   public static void main(String args[]) {		
      File file = new File("E://Name.txt");
      FileReader fr = new FileReader(file); 
   }
}

 

Output: 

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Unhandled exception type FileNotFoundException

	at Test.main(Test.java:8)
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is an Unchecked Exception?

  • An unchecked exception occurs during the execution process.
  • Unchecked Exceptions are also known as runtime exceptions.
  • Programming mistakes, such as logic errors or improper use of an API, are examples of this.
  • Runtime exceptions are ignored during compilation.

Example

If a 7-element array is declared and the programme attempts to access its 8th element, an ArrayIndexOutOfBoundsExceptionexception occurs.

Here, the below program will be successfully compiled, but it’ll throw an exception at runtime.

public class Test{

	public static void main(String args[]) {
	      int num[] = {100, 20, 30, 40, 50, 60, 70};
	      System.out.println(num[7]);
	   }
	}


Output:

Exception in thread "main" java. Lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 7
	at Test.main(Test.java:7)

Differences between Checked and Unchecked Exceptions in Java

The table below shows some of the notable differences between checked and unchecked exceptions in Java.

Key Factors Checked Exceptions Unchecked Exceptions
Occurrence Checked exceptions occur at compile time. Unchecked exceptions occur at runtime.
Role of Compiler The compiler checks a checked exception at compile time. The compiler does not check these exceptions.
Handling Handled at compile time. Handled at run time.
Parent Class Exception class is the direct parent class of the checked exception subclass. RuntimeException is the direct parent class of the Unchecked exception subclass.
Reason for Occurrence A checked Exception occurs when the chances of failure are too high. RuntimeException is the direct parent class of the Unchecked exception subclass.
Example SQLException, IOException, ClassNotFoundException, InvocationTargetException NullPointerException, ArrayIndexOutOfBoundsException, ArithmeticException, IllegalArgumentException,NumberFormatException

You might think of exceptions and errors as words that are analogous to each other. But they are not!

Frequently Asked Questions

What is the difference between a checked and unchecked exceptions?

Checked exceptions must be handled explicitly in code, while unchecked exceptions, including RuntimeExceptions, do not require explicit handling, typically indicating programming errors or system failures.

What is the difference between RuntimeException and exception in Java?

RuntimeException is a subclass of Exception in Java, representing unchecked exceptions that don't need to be declared or caught, unlike checked exceptions which require explicit handling.

What is checked and unchecked exception in C++?

In C++, exceptions are generally unchecked. Unlike Java, there's no explicit distinction between checked and unchecked exceptions. All exceptions in C++ are typically handled through try-catch blocks.

What is the type of unchecked exception?

Unchecked exceptions in Java include subclasses of RuntimeException and Error. These exceptions don't need to be declared or caught explicitly, indicating critical errors or programming mistakes.

Conclusion

In the checked exception vs unchecked exception, we learned that checked exceptions occur at compile time when the chances of failure are too high. In contrast, unchecked exceptions arise at runtime primarily due to syntax mistakes.

Eventually, after knowing the causes, types, and key differences between checked exception vs unchecked exception, you should start looking for ways to handle these exceptions in Java.

Java is a powerful general-purpose, object-oriented programming language used to develop mobile applications, web applications, big data processing, and embedded applications and is supported by billions of devices worldwide.

Aspire to become the next face of Java Developer? Don’t miss checking out our Java Foundation course with Data Structures & Algorithms.

If you need more hands-on experience in Java, you can practice problems on Coding Ninjas Studio. Here, you will get more than 2115 DSA problems frequently asked in interview rounds, along with the interview experiences of scholars in big product-based companies.

Previous article
Learn Core Java Programming For Free With Coding Ninjas
Next article
Working with Multiple Java Versions on Linux
Guided path
Free
gridgp-icon
Data structures & algorithms (Beginner to Intermediate)
13 chapters
109+ Problems
gp-badge
Earn badges and level up
Live masterclass