Checked and Unchecked Exception in Java
Extracts are divided into two main types: standard and unmarked extracts. The difference between these types mainly affects how they are handled by the compiler and the runtime environment.
Checked Exceptions
Checked exceptions are exceptions that are checked at compile-time. This means that the compiler ensures that these exceptions are declared caught or thrown by a possible method. If a method throws a tried exception, the caller must either handle the exception (using try-catch) or propagate it to the calling stack (by declaring it in a method signature that throws).
Examples of Checked Exceptions
IOException- This exception is thrown when there is a problem reading or writing from a file or network.
Example-
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Example {
public static void main(String[] args) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("file.txt"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
In this example, the FileReader
and readLine()
methods can throw an
IOException
, which is an observed exception.
SQLException- This exception is thrown when an error occurs in the database.
Exception
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Example {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "username", "password");
stmt = conn.createStatement();
// execute SQL queries
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Here the getConnection()
method can throw a SQLException.
Unchecked Exceptions (Runtime Exceptions)
Unchecked objects, also called runtime exceptions, need not be declared in a method's
throws
clause, and are not handled by the compiler These exceptions are often caused by programming errors, such as an array going out of bounds, call a method on a
null
object reference, or divide by zero
Examples of Unchecked Exceptions
NullPointerException- This exception occurs when you try to call a method or access a variable on a
null
object reference.
public class Example {
public static void main(String[] args) {
String str = null;
System.out.println(str.length()); // This will throw NullPointerException
}
}
ArrayIndexOutOfBoundsException- This exception occurs when you try to access an array element with an incorrect index.
public class Example {
public static void main(String[] args) {
int[] arr = new int[5];
System.out.println(arr[10]); // This will throw ArrayIndexOutOfBoundsException
}
}
Key Differences
Handling Requirement
- Observed exceptions must be caught with the try-catch block or reported as thrown with the
throws
keyword in the method signature. - Unhandled exceptions need not be caught explicitly or explicitly.
Examples
- The exceptions reviewed typically involve external resources such as IO processing, network connectivity, or database issues.
- Unchecked exceptions often indicate systematic errors or unexpected circumstances.
Propagation
- Checked exceptions expand the call stack until they are caught.
- Unchecked exceptions expand the call stack, but need not be expanded in method signatures.
Understanding these differences helps establish robust exception handling procedures in Java applications, ensuring that expected errors are handled properly, and properly resolving unexpected events.
Also, Read: Factory vs Abstract Factory Design Patterns in Java
Leave Comment