What is an example of a checked exception?

Improve Article

Save Article

  • Read
  • Discuss
  • Improve Article

    Save Article

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

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

    What is an example of a checked exception?

    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.

    Implementation: Consider GFG.txt file does not exist.

    Example 1-A

    Java

    import java.io.*;

    class GFG {

        public static void main(String args[]) {

            FileInputStream GFG

                = new FileInputStream("/Desktop/GFG.txt");

        }

    }

     Output:

    What is an example of a checked exception?

    Now let us do discuss how tohandle FileNotFoundException. The answer is quite simple as we can handle it with the help of a try-catch block 

    • Declare the function usingthe throw keyword to avoid a Compilation error.
    • All the exceptions throw objects 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.

    Example 1-B:

    Java

    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");

            }

        }

    }

    Output

    File does not exist

    Now let us discuss one more checked exception that is ClassNotFoundException. This Exception occurs when methods like Class.forName() and LoadClass Method etc. are unable to find the given class name as a parameter.

    Example 2-A

    Java

    import java.io.*;

    class GFG {

        public static void main(String[] args)

        {

            Class temp = Class.forName("gfg");

        }

    }

     Output:

    What is an example of a checked exception?

    Again now let us handle ClassNotFoundException using try-Catch block 

    Example 2-B

    Java

    import java.io.*;

    class GFG {

        public static void main(String[] args)

            throws ClassNotFoundException

        {

            try {

                Class temp = Class.forName(

                    "gfg");

            }

            catch (ClassNotFoundException e) {

                System.out.println(

                    "Class does not exist check the name of the class");

            }

        }

    }

    Output

    Class does not exist check the name of the class


    What is checked and unchecked exception give example?

    A checked exception is caught at compile time whereas a runtime or unchecked exception is, as it states, at runtime. A checked exception must be handled either by re-throwing or with a try catch block, whereas an unchecked isn't required to be handled.

    What are all checked exceptions?

    Common checked exceptions include IOException, DataAccessException, InterruptedException, etc. Common unchecked exceptions include ArithmeticException, InvalidClassException, NullPointerException, etc. 6. These exceptions are propagated using the throws keyword.

    What are examples of exceptions?

    Most Common Java Exceptions.
    NullPointerException..
    ArrayIndexOutOfBoundsException..
    IllegalStateException..
    ClassCastException..
    ArithmeticException..
    IllegalArgumentException..

    What are all the checked exceptions in Java?

    Some common checked exceptions in Java are IOException, SQLException and ParseException.