In Java, a checked exception is an exception that is checked at compile time. This means that the code must handle the exception or declare it to be thrown. Checked exceptions typically represent errors that can be anticipated and handled gracefully.
Here is an example of a checked exception:
import java.io.*;
public class FileRead {
public static void main(String[] args) {
try {
File file = new File("example.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
} catch (IOException e) {
System.out.println("An error occurred while reading the file: " + e.getMessage());
}
}
}
In this example, we are attempting to read from a file named "example.txt". This operation can potentially throw an IOException, which is a checked exception. To handle this exception, we wrap the file reading code in a try-catch block that catches the IOException and prints an error message.
If the file does not exist or is not readable, an IOException will be thrown, and the catch block will handle it gracefully. Without the try-catch block, the compiler would complain that the IOException is not handled, since it is a checked exception.
Checked exceptions are useful for forcing developers to handle errors that may occur during program execution, and for providing more robust error handling in Java programs.
there are several types of checked exceptions that can be thrown during program execution. Some common types of checked exceptions are:
IOException: This exception is thrown when an input or output operation fails or is interrupted. This can happen, for example, if a file cannot be found or opened, if a network connection is lost, or if a device is disconnected.
try {
BufferedReader reader = new BufferedReader(new FileReader("myfile.txt"));
String line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
SQLException: This exception is thrown when an error occurs during database access or manipulation. This can happen, for example, if a SQL query is invalid, if a connection to the database cannot be established, or if a transaction fails.
try {
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM customers");
} catch (SQLException e) {
e.printStackTrace();
}
ClassNotFoundException: This exception is thrown when a class that is referenced in the code cannot be found. This can happen, for example, if a class is misspelled or if a required library is missing.
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
InterruptedException: This exception is thrown when a thread is interrupted while it is waiting or sleeping. This can happen, for example, if another thread calls the interrupt() method on the waiting thread.
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
InvocationTargetException: This exception is thrown when a method that is invoked through reflection throws an exception. This can happen, for example, if the method being invoked has a bug or a runtime error.
try {
Method method = obj.getClass().getMethod("myMethod", String.class);
method.invoke(obj, "hello");
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
These are just a few examples of the many types of checked exceptions that can be thrown in Java. It is important for developers to be familiar with these exceptions and to handle them gracefully in their code, either by catching and handling them, or by declaring them to be thrown.