Advertisement



< Prev
Next >



Checked Exceptions





What are Checked Exceptions?


During the compile-time of a program, if the Java Compiler feels that an exception may arise due to the execution of some statements in a program, it notifies us about the possibility of this exception in the form of compile-time error and asks us to handle the possible exception. These exceptions are called checked exceptions because they are checked by the compiler during the compile time of a program.

Checked Exceptions are never thrown at the compile time but they are only "notified" to us at the compile time in the form of a compile-time errors. By issuing these compile-time errors, Java Compiler asks us to handle these probable checked exceptions in our program in advance, so that our program can recover from these exceptions if they are actually thrown at the program runtime.





Some examples of Checked Exceptions


Checked Exceptions Description
ParseException ParseException is thrown when we try to parse a badly formatted String or when there is an error in parsing a String.
IOException I/O Exception is generated when we try to perform an input/output i.e. read/write operation and when the outcome is a failed or interrupted input/output operation.
FileNotFoundException This Exception is thrown when we are trying to access a file by specifying its path, for the purpose of reading it but this file is not found.
ClassNotFoundException This Exception is thrown when an our code tries to load in a class through its name but no definition for the class with the specified name could be found.
InterruptedException This Exception is thrown when a thread is waiting, sleeping and it has been interrupted either before or during this process.

Let's see how compiler checks for checked exceptions and send us a compile-error in the next section below.




Java Compiler checking for checked exception - FileNotFoundException


Java Compiler checks one of the checked exceptions, FileNotFoundException when a program tries to open a file that already exists in our computer to read, leading Java Compiler to send a compile-time error.

import java.io.*;
 
class CheckedEx			//Class begins
{
public static void main(String... ar)			//main method begins
{
	File file= new File("File1.txt");
	FileReader fileReader= new FileReader(file);
} 		//main method ends
} 		//class ends


Compiling this program results in an compilation error -:


D:\Java\javac CheckedEx.java
CheckedEx.java:8: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
FileReader fileReader= new FileReader(file);
			^
1 error

This compile error says that a checked exception of type FileNotFoundException could be thrown at the runtime of this program, hence this exception should be caught/handled within this program. Like this, compiler checks for a checked exception and asks us to handle it during compile-time of the program, so that our program doesn't end abruptly, if this exception is actually thrown at the runtime.


Advertisement




Java Compiler checking for checked exception - ParseException


Java Compiler checks one of the checked exceptions, ParseException when a program tries to convert a String to Date object using the parse() method of DateFormat class, this conversion leads Java Compiler to send a compile-time error.

import java.util.*;
import java.text.*;

class A
{
public static void main(String... ar)
{

	//Converting a String to Date object.
	DateFormat df = DateFormat.getInstance();
	Date d= new Date(10000000000L);		//creating a Date object
	String str= df.format(d);		//Converting Date object to String
	Date d2= df.parse(str);			//Converting a String to Date object.
	System.out.println(d2);
}
}


Compiling this program results in an compilation error -:

A.java:14: error: unreported exception ParseException; must be caught or declared to be thrown
        Date d2= df.parse(str);                 //Converting a String to Date object.
                         ^
1 error

This compile error says that a checked exception of type ParseException could be thrown at the execution of statements in this program at runtime, hence this exception should be caught/handled within this program, before this program is successfully compiled and made to run.



Please share this article -





< Prev
Next >
< What is Exception?
Unchecked Exception >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement