Difference between checked and unchecked exception in java
In Java, exceptions are categorized into two main types: checked exceptions and unchecked exceptions.
Checked Exceptions:
Checked exceptions are exceptions that are checked at compile-time. This means that the compiler forces you to handle these exceptions either by using a try-catch
block or by declaring the exception using the throws
keyword in the method signature. Checked exceptions typically represent external issues that a program should anticipate and recover from, such as file not found, database connectivity issues, or network problems.
Example of Checked Exception:
/*
* Author: Zameer Ali Mohil
* */
import java.io.FileReader;
import java.io.FileNotFoundException;
public class CheckedExample {
public static void main(String[] args) {
try {
FileReader file = new FileReader("file.txt"); // This statement can throw FileNotFoundException
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
}
}
}
In this example, FileReader
constructor can throw a FileNotFoundException
, which is a checked exception. The compiler forces you to handle this exception using a try-catch
block.
Unchecked Exceptions (Runtime Exceptions):
Unchecked exceptions, also known as runtime exceptions, are exceptions that are not checked at compile-time. These exceptions occur at runtime and are usually caused by programming bugs, such as null pointer access or dividing by zero. Unchecked exceptions are subclasses of RuntimeException
and its subclasses.
Example of Unchecked Exception:
/*
* Author: Zameer Ali Mohil
* */
public class UncheckedExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
System.out.println(numbers[4]); // This statement can throw ArrayIndexOutOfBoundsException
}
}
In this example, accessing numbers[4]
can throw an ArrayIndexOutOfBoundsException
, which is an unchecked exception. You are not forced by the compiler to handle this exception, although you should handle it to ensure your program’s robustness.
Key Differences:
- Checked Exceptions:
- Checked exceptions are checked at compile-time.
- These exceptions extend
Exception
class but notRuntimeException
. - You are forced to handle checked exceptions using
try-catch
orthrows
keywords. - Examples include
IOException
,SQLException
, etc.
- Unchecked Exceptions:
- Unchecked exceptions are not checked at compile-time.
- These exceptions extend
RuntimeException
class. - You are not forced to handle unchecked exceptions, although it’s a good practice to handle them for program robustness.
- Examples include
NullPointerException
,ArrayIndexOutOfBoundsException
,ArithmeticException
, etc.
In general, checked exceptions represent conditions that a well-written application should anticipate and recover from, while unchecked exceptions typically represent programming errors that should be fixed. Unchecked exceptions provide more flexibility to programmers but require careful handling to ensure the robustness of the application.