Checked Exceptions in JAVA

Checked Exceptions in JAVA

Checked Exceptions

Checked Exceptions are the exceptions that we can typically foresee and plan ahead in our application. These are also exceptions that the Java Compiler requires us to either handle-or-declare when writing code.

The handle-or-declare rule refers to our responsibility to either declare that a method throws an exception up the call stack - without doing much to prevent it or handle the exception with our own code, which typically leads to the recovery of the program from the exceptional condition.

This is the reason why they're called checked exceptions. The compiler can detect them before runtime, and you're aware of their potential existence while writing code.

For example FileNotFoundException occurs if File that needs to be opened is not found.

Checked Exceptions Description
ClassNotFoundException Occurs when class is not found in classpath.
FileNotFoundException  Occurs when file is not found
InterruptedException This exception is thrown when a thread is interrupted.

1. ClassNotFoundException

The ClassNotFoundException is thrown when the Java Virtual Machine (JVM) tries to load a particular class and the specified class cannot be found in the classpath. This exception is thrown when an application tries to load a class through its string name, but no definition for the specified class name could be found. Most of the time this exception will occur when you try to run application without updating classpath with JAR files.

Program(Save as ClassNotFound.java)

import java.util.*;
class ClassNotFound
{
    public static void main(String args[])
    {
        try{
            Class loadClass = Class.forName("CodeDost");
            System.out.println("Class " + loadClass + " found successfully!!!");
        }
        catch(ClassNotFoundException e){
            e.getMessage();
            System.out.println(e+" occurred!");
        }
    }
}

Output

javac ClassNotFound.java
java ClassNotFound
java.lang.ClassNotFoundException: CodeDost occurred!

In the above program ClassNotFoundException occurs as class "Codedost" is not found in classpath.

If we change "CodeDost" to "java.util.Scanner", we will see that class is found successfully as we have imported package util.* which includes all the classes under the util package.

2. FileNotFoundException

This Exception occurs when a file is not accessible or does not open.

Program(Save as FileNotFound.java)

import java.io.*;
class FileNotFound
{
    public static void main(String args[])
    {
        try{
            File file = new File("codedost.txt");
            FileReader fr = new FileReader(file);
            System.out.println("File " + file + " found successfully!!!");
        }
        catch(FileNotFoundException e) { 
            e.getMessage();
            System.out.println(e+" occurred!");
        }
    }
}

Output

javac FileNotFound.java
java FileNotFound
java.io.FileNotFoundException: codedost.txt (The system cannot find the file specified) occurred!

In the above program as we have not created any file named "codedost.txt", hence FileNotFoundException occurs. Just create a file named "codedost.txt" and we will get file found successfully as output.

3. InterruptedException

This exception is thrown when a thread is interrupted. The thread could be in either waiting, sleeping or running state and this exception can be thrown either before or during a thread’s activity.

Program

class Interrupted implements Runnable
{
    public void run() //Entry Point of the new thread.
    {
        try
        {
            //This will make the thread(Thread2) sleep for 1000ms.
            Thread.sleep(1000); 
        }
        catch(InterruptedException e)
        {
            e.getMessage();
            System.out.println("Thread is interrupted - " +e);
        } 
    }

    public static void main(String args[])
    {
        Interrupted newT= new Interrupted();
        Thread t= new Thread(newT,"Thread2");

        //Starts the thread(Thread2)
        t.start(); 
        //Main thread interrupting the thread(Thread2) while it is sleeping, causing InterruptedException
        t.interrupt(); 
    }
}

Output

javac Interrupted.java
java Interrupted
Thread is interrupted - java.lang.InterruptedException: sleep interrupted

In the above program, in the main() method, we have created a new thread with the name, Thread2t.start() starts the Thread2, which starts executing its run() method. In run() method, the sleep () method is called on the Thread2 to make it sleep for 1 second.While the Thread2 is sleeping, the main thread calls interrupt() on it, which interrupts its sleep and it throws an InterruptedException.

Share Me!
  •  
  •  
  •  
  •  
  •  
  •