0% found this document useful (0 votes)
63 views

Exception Handling

Exception handling in Java allows programs to recover from problems and unexpected situations. There is a hierarchy of exceptions with Throwable at the top, and Exception and RuntimeException subclasses. Checked exceptions must be declared or handled, while unchecked exceptions like NullPointerException may be left unhandled. Try/catch blocks identify code that could throw exceptions, with catch providing recovery. Finally blocks always execute regardless of exceptions. Multiple catch blocks can handle different exception types in order of specificity.

Uploaded by

Pritesh Kakani
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Exception Handling

Exception handling in Java allows programs to recover from problems and unexpected situations. There is a hierarchy of exceptions with Throwable at the top, and Exception and RuntimeException subclasses. Checked exceptions must be declared or handled, while unchecked exceptions like NullPointerException may be left unhandled. Try/catch blocks identify code that could throw exceptions, with catch providing recovery. Finally blocks always execute regardless of exceptions. Multiple catch blocks can handle different exception types in order of specificity.

Uploaded by

Pritesh Kakani
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Exception Handling

Exceptions and Errors


When a problem encounters and unexpected
termination or fault, it is called an exception
When we try and divide by 0 we terminate
abnormally.
Exception handling gives us another
opportunity to recover from the abnormality.
Sometimes we might encounter situations
from which we cannot recover like
Outofmemory. These are considered as
errors.
Hierarchy
Hierarchy
Throwable class is a super class of all
exception and errors.
Exceptions has a special subclass,
the RuntimeException
A user defined exception should be a
subclass of the exception class
Checked and Unchecked
Exception
Exceptions which are checked for during compile time
are called checked exceptions.
Example: SQLException or any userdefined exception
extending the Exception class
Exceptions which are not checked for during compile
time are called unchecked exception.
Example: NullPointerException or any class extending
the RuntimeException class.
All the checked exceptions must be handled in the
program.
The exceptions raised, if not handled will be handled
by the Java Virtual Machine. The Virtual machine will
print the stack trace of the exception indicating the
stack of exception and the line where it was caused.
Example
public class myexception{
public static void main(String args[]){
try{
File f = new File(myfile);
FileInputStream fis = new FileInputStream(f);
}catch(FileNotFoundException ex){
File f = new File(Available File);
FileInputStream fis = new FileInputStream(f);
}
finally{
// the finally block
}
//continue processing here.
}
}
In this example we are trying to open a file and if the file does not exists we can do further
processing in the catch block.
The try and catch blocks are used to identify possible exception conditions. We try to execute
any statement that might throw an exception and the catch block is used for any exceptions
caused.
If the try block does not throw any exceptions, then the catch block is not executed.
The finally block is always executed irrespective of whether the exception is thrown or not.
Using throws clause
If you dont want the exception to be handled in the same function you can use the
throws class to handle the exception in the calling function.
public class myexception{
public static void main(String args[]){
try{
checkEx();
} catch(FileNotFoundException ex){
}
}
public void checkEx() throws FileNotFoundException{
File f = new File(myfile);
FileInputStream fis = new FileInputStream(f);
//continue processing here.
}
}

In this example, the main method calls the checkex() method and the checkex
method tries to open a file, If the file in not available, then an exception is
raised and passed to the main method, where it is handled.
Catching Multiple
exceptions
public class myexception{
public static void main(String args[]){
try{
File f = new File(myfile);
FileInputStream fis = new FileInputStream(f);
}
catch(FileNotFoundException ex){
File f = new File(Available File);
FileInputStream fis = new FileInputStream(f);
}catch(IOException ex){
//do something here
}
finally{
// the finally block
}
//continue processing here.
}
}
We can have multiple catch blocks for a single try statement. The exception handler looks for a
compatible match and then for an exact match. In other words, in the example, if the exception
raised was myIOCustomException, a subclass of FileNotFoundException, then the catch block of
FileNotFoundExeception is matched and executed.
If a compatible match is found before an exact match, then the compatible match is preferred.
We need to pay special attention on ordering of exceptions in the catch blocks, as it can lead to
mismatching of exception and unreachable code.
We need to arrange the exceptions from specific to general.
Further Reading
http://docs.oracle.com/javase/tutorial
/essential/exceptions/
http://www.tutorialspoint.com/java/ja
va_exceptions.htm

You might also like