Skip to content
Related Articles

Related Articles

Java Program to Handle Checked Exception
  • Difficulty Level : Expert
  • Last Updated : 29 Oct, 2020

Checked exceptions are the subclass of the Exception class. These types of exceptions occur during the compile time of the program. These exceptions can be handled by the try-catch block otherwise the program will give a compilation error.

 ClassNotFoundException, IOException, SQLException etc are the examples of the checked exceptions.

 
 

Exceptions in Java

I/O Exception: This Program throw I/O exception because of due FileNotFoundException is a checked exception in Java. Anytime, we want to read a file from the file system, Java forces us to handle error situations where the file is not present in the given location.



Consider myfile.txt file does not exist.
 

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to Handle Checked Exception
import java.io.*;
class GFG {
    public static void main(String args[])
    {
        FileInputStream GFG
            = new FileInputStream("/home/mayur/GFG.txt");
        // this file does not exist in the location
        /* This constructor FileInputStream
         * throws FileNotFoundException which
         * is a checked exception
         */
    }
}

chevron_right


Output:

Handling FileNotFoundException: 

This exception can be handled with the help of a try-catch block 

  • Declare the function using the throw keyword to avoid a Compilation error.
  • All the exceptions throw object when they occur try statement allows you to define a block of code to be tested for errors and catch block captures the given exception object and perform required operations.
  • Using a try-catch block defined output will be shown.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to Handle Checked Exception
import java.io.*;
import java.util.*;
class GFG {
    public static void main(String[] args)
        throws FileNotFoundException
    {
        FileInputStream GFG = null;
        try {
            GFG = new FileInputStream(
                "/home/mayur/GFG.txt");
        }
        catch (FileNotFoundException e) {
            System.out.println("File does not exist");
        }
    }
}

chevron_right


Output:



ClassNotFoundException: 

This Exception occurs when methods like Class.forName() and LoadClass Method etc unable to find the given class name as a parameter.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to Handle Checked Exception
import java.io.*;
class GFG {
    public static void main(String[] args)
    {
        Class temp = Class.forName("gfg");
        // Calling the clas gfg which is not present in the
        // current class temp instance of calling class it
        // will throw ClassNotFoundException;
    }
}

chevron_right


Output:

Handling ClassNotFoundException: Using try-Catch Block 

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to Handle Checked Exception
import java.io.*;
class GFG {
  
    public static void main(String[] args)
        throws ClassNotFoundException
    {
  
        try {
            Class temp = Class.forName(
                "gfg"); // calling the gfg class
        }
        catch (ClassNotFoundException e) {
            // block executes when mention exception occur
            System.out.println(
                "Class does not exist check the name of the class");
        }
    }
}

chevron_right


Output:


Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :