Checked vs Unchecked Exceptions in Java
# Checked vs Unchecked Exceptions in Java
Java provides a powerful and robust exception handling mechanism which allows users to handle errors and exceptions efficiently. In Java, there are two types of exceptions - checked and unchecked exceptions.
Checked exceptions are those which are checked at compile-time. These exceptions are checked by the compiler to ensure that the code has appropriate exception handling. Examples of checked exceptions are IOException, SQLException, ClassNotFoundException, etc. All these exceptions are checked by the compiler at the time of compilation and the programmer needs to provide necessary code to handle the exception.
Unchecked exceptions, on the other hand, are not checked by the compiler. These exceptions are not checked by the compiler at the time of compilation, but are thrown during the execution of the program. Examples of unchecked exceptions are ArithmeticException, ArrayIndexOutOfBoundsException, NullPointerException, etc. These exceptions can be handled by the programmer, but it is not compulsory.
Checked exceptions are generally used to signal conditions that a reasonable application might want to catch, while unchecked exceptions are used to signal programming errors and other conditions that are outside the control of the application. It is important to understand the difference between checked and unchecked exceptions, as this will help you to handle errors and exceptions in Java effectively.
Worried About Failing Tech Interviews?
Attend our webinar on
"How to nail your next tech interview" and learn
.png)
Hosted By
Ryan Valles
Founder, Interview Kickstart

Our tried & tested strategy for cracking interviews

How FAANG hiring process works

The 4 areas you must prepare for

How you can accelerate your learnings
Register for Webinar
###Checked Exceptions in Java
Checked exceptions are exceptions which are checked at compile-time. This means that the code which could potentially throw a checked exception must either handle the exception or be declared to throw the exception. This is done to ensure that the code that could potentially throw an exception is prepared to handle the exception.
A simple example of a checked exception in Java is the IOException. This exception is thrown when an input/output operation fails.
###Sample Code
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class CheckedExceptions {
public static void main(String[] args) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("file.txt"));
String line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();
}
} catch (IOException e) {
System.out.println("An I/O error occurred.");
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
System.out.println("Error closing BufferedReader.");
}
}
}
}
```
###Unchecked Exceptions in Java
Unchecked exceptions are exceptions which are not checked at compile-time. This means that the code which could potentially throw an unchecked exception does not need to handle the exception or declare it to be thrown.
A simple example of an unchecked exception in Java is the NullPointerException. This exception is thrown when a reference to an object is null and the code attempts to call a method on the object.
###Sample Code
```java
public class UncheckedExceptions {
public static void main(String[] args) {
String str = null;
System.out.println(str.length());
}
}
```