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:
Checked exceptions
Unchecked exceptions
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)
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.
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.
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.