Java Exception 12
Java Exception 12
Java Exceptions
Intro to Exceptions
Exception Error
many RuntimeException
IndexOutOfBounds NullPointerException
Throwable hierarchy
All exceptions extend the class Throwable,
which splits into two branches:
Error and Exception
Error: internal errors and resource
exhaustion inside the Java runtime system.
Little you can do.
Exception: splits further into two branches.
RuntimeException and other exceptions
Focus on the Exception branch
Two branches of Exception
exceptions that derived from RuntimeException
examples: a bad cast, an out-of-array access
happens because errors exist in your program. Your
fault.
those not in the type of RuntimeException
example: trying to open a malformed URL
program is good, other bad things happen. Not your
fault.
Focus on the Exception branch
Checked exceptions vs. unchecked exceptions
Unchecked exceptions: exceptions derived from
the class Error or the class
RuntimeException
Checked exceptions: all other exceptions that
are not unchecked exceptions
If
they occur, they must be dealt with in some
way.
The compiler will check whether you provide
exception handlers for checked exceptions
which may occur
Approaches to handling an exception
1. Prevent the exception from happening
2. Catch it in the method in which it occurs, and either
a. Fix up the problem and resume normal execution
b. Rethrow it
c. Throw a different exception
3. Declare that the method throws the exception
4. With 1. and 2.a. the caller never knows there was an
error.
5. With 2.b., 2.c., and 3., if the caller does not handle the
exception, the program will terminate and display a
stack trace
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
Use the throws 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.
}
}
Using throws clause
• 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.
}
}
Catching Multiple exceptions
• 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.
Exception Handling Basics
Three parts to Exception handling
1. claiming exception
2. throwing exception
3. catching exception
try
{
foo();
}
catch(SomeException se)
{
...
}
Example1
import java.io.*;