Checked Exception In Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

What is a Checked Exception in Java ?

Checked exceptions are the ones that are checked during compilation time. If a code within a method throws a checked exception, then it should either be handled by a method or specify it using the throws keyword.

Types of Checked Exception

  • ClassNotFoundException :
    It is a kind of checked exception that occurs when a Java virtual machine (JVM) tries to load a class but fails because it is unable to find the classpath. Since it is a checked exception, it should be handled explicitly by using the try-catch block or by using the throws keyword.
  • InterruptedException :
    It usually occurs when a thread is interrupted while waiting or sleeping or occupied in some task. During this, a method interrupt() is been called by the code in the thread which blocks the I/O operations.
  • IOException :
    It is an input/output exception and it occurs whenever an input or output operation fails or is interpreted in java. For instance, when you try to read a file that does not exist, it will throw an IOException.
  • SQLException :
    This exception provides detail about SQL database error if it occurs. The error can be SQL syntax or SQL driver.
  • FileNotFoundException :
    It occurs when a file that we are trying to find is unavailable in the directory. It is available in class java.io and is a checked exception because it is thrown by the constructor RandomAccessFile at run time.

Example

This is an example of FileNotFoundException

Java Program to Handle FileNotFound Checked Exception :

Output :

Explanation :
If the given file is not found in the directory (classpath) as mentioned, then it will give an exception FileNotFoundException.

This is an example of ClassNotFoundException

Code :

Output :

Explanation :
A class newProg is not found in the code hence an exception is thrown.

How to Handle Checked Exceptions ?

Checked exceptions should be handled properly for the execution of code. There are two ways in which exceptions in java can be handled.

Method - 1 :
Declare the exception using the throws keyword (throws).
Method - 2 :
Handle them using try-catch blocks (try-catch).

Throws :

A keyword throws is used for handling the exceptions. It throws the exception up to the stack to call a method called handle. It is a great way to handle exceptions because it helps code look clean and handle exceptions properly.

Syntax :

Try-catch Block :

The code is simply wrapped in a try-catch block to handle the exception. It throws the exception within the try-catch block.

Syntax :

When to Use Checked Exception in Java ?

Checked exceptions should be used in case of predictable events and to prevent unpreventable events. The code can be recovered from an exception if it's a checked exception. If the error looks like it can be solved using a checked exception then a checked exception is used. It is mainly used for testing the edge case of the code.

Usually, when software is built, it goes into the testing phase. This is where the checked exceptions come in handy.

Are Checked Exceptions Good Or Bad ?

Well, this is a controversial question and it depends on the programmer's use case. There can be certain cases wherein checked exceptions can be used and some cases where unchecked exceptions are preferred. Whatever the case, exceptions need to be handled properly for preventing the code from being crashed.

Learn More

Checked and unchecked exceptions in java

Conclusion

I hope you liked this blog. Here are a few key takeaways from this blog.

  • Checked exceptions are the ones that are checked during compilation time.
  • Types of Checked Exception :
    • ClassNotFoundException
    • InterruptedException
    • IOException
    • SQLException
    • FileNotFoundException
  • Handle Checked Exceptions :
    • Method - 1 :
      Declare the exception using the throws keyword (throws).
    • Method - 2 :
      Handle them using try-catch blocks (try-catch).
  • Checked exceptions should be used in case of predictable events and to prevent unpreventable events. The code can be recovered from an exception if it's a checked exception.