All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Checked Exception In Java

Last Updated : Mar 11, 2024

Checked Exception In Java

In this article we will show you the solution of checked exception in java, these are the exceptions that the compiler looks for during compilation.

The method must either handle the exception or specify it using the throws keyword if any code within it throws a checked exception.

There are two types of checked exceptions: fully checked and partially checked exceptions.

Fully checked exceptions, such as IOException and InterruptedException, are checked exceptions that also have all of their descendant classes verified.

A checked exception that has parts of its child classes, such as Exception, unchecked is referred to as a partially checked exception.

Step By Step Guide On Checked Exception In Java :-

For illustration, take a look at the Java programme below, which opens the file "C:testa.txt" and outputs the first three lines of it.

Because the function main() calls FileReader(), which throws the checked exception FileNotFoundException, the programme cannot be compiled.

Additionally, it employs the readLine() and close() methods, both of which raise checked exceptions IOException.

In Java, a checked exception denotes a foreseeable, incorrect circumstance that may arise even when a software library is utilized according to plan.

For instance, the Java IO library has developers deal with the checked FileNotFoundException when they attempt to read a file.

A checked exception was made by the creators of the Java IO API in order to make a developer deal with the rather frequent occurrence of trying to access a file that doesn't exist.

In Java, checked exceptions are those that are checked at the time of compilation.

In order to determine whether a method that throws an exception contains the code to handle the exception with a try-catch block or not, the Java compiler checks the checked exceptions during compilation.

The compiler also checks to see if the method is specified with the throws keyword if there is no code to handle them. Additionally, the compiler reports a compilation error if neither of the two situations is found.

An exception that is caught during compilation is referred to as a checked exception or a compile time exception.

At the time of compilation, these exceptions cannot simply be disregarded; the programmer must address them.

Simply place a try catch block around the Java code that causes the checked exception to be thrown.

You may now process and handle the exception as a result of this. This method makes it very simple to ignore the exception and continue as normal.

You might encounter our good friend the NullPointerException later in the code when what the method was performing is necessary.

class GFG {
public static void main(String[] args)
FileReader file = new FileReader("C:\\test\\a.txt");
BufferedReader fileInput = new BufferedReader(file);
for (int counter = 0; counter < 3; counter++)
  1. Importing the I/o classes.
  2. Creating the main Class
  3. Next step, Main driver method
  4. Reading file from path in local directory using FileReader file = new FileReader("C:\\test\\a.txt");
  5. Next step, creating objects as one way of taking input and using iteration for loop.
  6. Lastly, closing the connections using fileInput.close();
  7. Run the code

Conclusion :-

So, this is the end of Checked exception tutorials in Java. In the next topic we will learn about unchecked exceptions in Java. Till that time, you can implement the code in command prompt.

I hope this article on checked exception in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪