In Java, Exception Can Be Checked or Unchecked. They Both Fit Into A Class Hierarchy. The Following Diagram Shows Java Exception Classes Hierarchy
In Java, Exception Can Be Checked or Unchecked. They Both Fit Into A Class Hierarchy. The Following Diagram Shows Java Exception Classes Hierarchy
In Java, exception can be checked or unchecked. They both fit into a class hierarchy. The
following diagram shows Java Exception classes hierarchy.
The rationale behind the hierarchy is as follows:
Exception subclasses represent errors that a program can reasonably recover from.
Except for RuntimeException and its subclasses (see below), they generally represent
errors that a program will expect to occur in the normal course of duty: for example,
network connection errors and filing system errors.
Error subclasses represent "serious" errors that a program generally shouldn't expect to
catch and recover from. These include conditions such as an expected class file being
missing, or an OutOfMemoryError.
RuntimeException is a further subclass of Exception. RuntimeException and its
subclasses are slightly different: they represent exceptions that a program shouldn't
generally expect to occur, but could potentially recover from. They represent what are
likely to be programming errors rather than errors due to invalid user input or a badly
configured environment.
Lets understand this with this example: In this example we are reading the file myfile.txt and
displaying its content on the screen. In this program there are three places where an checked
exception is thrown as mentioned in the comments below. FileInputStream which is used for
specifying the file path and name, throws FileNotFoundException. The read() method which
reads the file content throws IOException and the close() method which closes the file input
stream also throws IOException.
import java.io.*;
class Example {
public static void main(String args[])
{
FileInputStreamfis = null;
/*This constructor FileInputStream(File filename)
* throws FileNotFoundException which is a checked
* exception*/
fis = new FileInputStream("B:/myfile.txt");
int k;
Output:
import java.io.*;
classExample{
publicstaticvoid main(Stringargs[])
{
FileInputStreamfis=null;
try{
fis=newFileInputStream("B:/myfile.txt");
}catch(FileNotFoundExceptionfnfe){
System.out.println("The specified file is not "+
"present at the given path");
}
int k;
try{
while(( k =fis.read())!=-1)
{
System.out.print((char)k);
}
fis.close();
}catch(IOExceptionioe){
System.out.println("I/O error occurred: "+ioe);
}
}
}
This code will run fine and will display the file content.
Here are the few other Checked Exceptions –
SQLException
IOException
DataAccessException
ClassNotFoundException
InvocationTargetException
class Example {
public static void main(String args[])
{
intarr[] ={1,2,3,4,5};
/*My array has only 5 elements but
* I'm trying to display the value of
* 8th element. It should throw
* ArrayIndexOutOfBoundsException*/
System.out.println(arr[7]);
}
}
This code would also compile successfully since ArrayIndexOutOfBoundsException is also an
unchecked exception.
class Example {
public static void main(String args[])
{
try{
intarr[] ={1,2,3,4,5};
System.out.println(arr[7]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("The specified index does not exist " +
"in array. Please correct the error.");
}
}
}
NullPointerException
ArrayIndexOutOfBoundsException
ArithmeticException
IllegalArgumentException