Javatpoint Logo
Javatpoint Logo

List of Checked Exceptions in Java

Exception handling is an essential aspect of programming, enabling developers to gracefully manage and recover from unforeseen errors. In Java, exceptions are categorized as checked or unchecked, with checked exceptions requiring explicit handling in the code. This article focuses on checked exceptions in Java, providing a comprehensive listing of typically encountered exceptions alongside pattern programs that exhibit their utilization.

1. FileNotFoundException:

The FileNotFoundException is thrown while an attempt to get right of entry to a record that does not exist or cannot be opened takes place. It is part of the java.io package. Consider the subsequent program:

Output:

File not found: nonexistent_file.txt (No such file or directory)

2. IOException:

The IOException class is a well-known exception that suggests a mistake passed off while acting enter/output operations. It is part of the java.io package. The following program demonstrates the coping with of an IOException:

Output:

An error occurred: nonexistent_file.txt (No such file or directory)

3. ClassNotFoundException:

The ClassNotFoundException is thrown whilst an strive is made to load a class dynamically the use of the Class.ForName() approach, but the detailed class can't be determined. Here's any other example:

ClassNotFoundExceptionExample.javas

Output:

Class not found: com.example.NonExistentClass

4. SQLException:

The SQLException is thrown when an error occurs while working with a database. It is part of the java.sql package. The code snippet below illustrates a basic scenario involving a database connection:

SQLExceptionExample.java

Output:

SQL error occurred: Communications link failure

5. ParseException:

The ParseException is thrown while an error occurs while parsing a string into a selected format, along with a date or more than a few. It is part of the java.text content package. Consider the following example:

ParseExceptionExample.java

Output:

Invalid date format: Unparseable date: "2023-13-45"

6. nterruptedException:

The InterruptedException is thrown when a thread is interrupted while it is in a sleeping or waiting state. It is often used to handle scenarios involving thread synchronization and concurrency. Here's an example:

InterruptedExceptionExample.java

Output:

(No output if the program runs without interruption. If interrupted, the program will display: "Thread interrupted: java.lang.InterruptedException: sleep interrupted")

7. MalformedURLException:

The MalformedURLException is thrown when an invalid or malformed URL is encountered. It is part of the java.net package. Consider the following program:

InterruptedExceptionExample.java

Output:

Malformed URL: no protocol: htp://example.com

8. ClassNotFoundException:

The ClassNotFoundException is thrown when an attempt is made to load a class dynamically using the Class.forName() method, but the specified class cannot be found. Here's another example:c

InterruptedExceptionExample.java

Output:

Class not found: com.example.NonExistentClass

Remember, these are just a few examples of checked exceptions in Java. Java provides a wide range of checked exceptions that cover various error scenarios. Understanding and handling these exceptions appropriately in your code will contribute to building robust and reliable applications.

In summary, understanding checked exceptions in Java is crucial for building robust and reliable applications. By being aware of the most common checked exceptions and how to handle them, developers can write code that gracefully recovers from errors and provides meaningful feedback to users. The sample programs provided in this article offer practical insights into handling checked exceptions effectively, helping you develop more resilient Java applications.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA