Checked Exceptions in Java

Previous
Next

Unchecked v/s Checked exceptions:

  • Exceptions classified into Checked and Unchecked.
  • Unchecked exceptions handling is optional.
  • Checked exceptions must be handled else compiler raises error.

Unchecked Exceptions:

  • The child class of RuntimeException is called Unchecked.
  • Examples…
    • ArithmeticException
    • NumberFormatException
    • NullPointerException
    • InputMismatchException
    • ….
  • In case of Unchecked, java program is not connected to any resource

Checked Exceptions:

  • Class which is not the Child class of RuntimeException.
  • Examples…
    • FileNotFoundException : java code connected to File
    • IOException : java code connected to IO device
    • SQLException : Java code connected to Database
    • ServletException : Java code connected to Server application.
  •  If we don’t handle the checked exception, compiler will not generate class file, hence we cannot run program.
  • After working with any resource in the application, we need to release that code properly.
  • Improper shutdown of resources causes loss of data.

Checked Exception code:

  • Opening a file in Read mode.
  • Java.io package is providing FileInputStream class to open a file in read mode.
class FileInputStream
{
	public FileInputStream(String name) throws FileNotFoundException
        {
	Input argument is “File path”
	If the file is present, it will open the file in read mode.
	Raises exception if the file is not present.
	“FileNotFoundException” is checked hence must be handled.
        }
}

We will get Compile time error in the below code as we are not handling exception.

import java.io.FileInputStream;
class OpenFile 
{
	public static void main(String[] args) 
	{
		String path = "e:/test.txt";
		FileInputStream file = new FileInputStream(path);
	}
}

We need to handle FileNotFoundException to avoid compile time error

import java.io.FileInputStream;
import java.io.FileNotFoundException;
class OpenFile 
{
	public static void main(String[] args) 
	{
		String path = "e:/test.txt";
		try
		{
			FileInputStream file = new FileInputStream(path);
			System.out.println("File opened...");
		}
		catch (FileNotFoundException e)
		{
			System.out.println("Exception : " + e.getMessage());
		}
	}
}

How to close the file?

Answer : Finally block is used to release the resource which is connected to program code.

/*
class FileInputStream
{
	public void close() throws IOException
	{
		Close the file.
	}
}
*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
class OpenFile 
{
	static FileInputStream file = null ;
	public static void main(String[] args) 
	{
		String path = "e:/test.txt";
		try
		{
			file = new FileInputStream(path);
			System.out.println("File opened...");
		}
		catch (FileNotFoundException e)
		{
			System.out.println("Exception : " + e.getMessage());
		}
		finally
		{
			try
			{
				file.close();
				System.out.println("File closed...");
			}
			catch (IOException io)
			{
				System.out.println("IO error");
			}
		}
	}
}
Previous
Next

Add Comment

Courses Enquiry Form