10 ExceptionHandlning
10 ExceptionHandlning
Exception Handling
2
Exceptions in Java
Exception:
An occurrence of an erroneous, unusual or unexpected event in a
program execution
In older languages
Code the handling of exceptions into each area of the program
that needed it
Some exceptions could not even be handled by the HLL
ex. standard Pascal cannot handle I/O errors or division by
0
Ask for integer and user enters a text string what do
you do?
2
Exceptions in Java
In newer languages
Exception handling built into the language
We can separate exception handling from the "main line"
code
Java uses an exception handling model similar to that used in C++
Exceptions are objects that are thrown and
catched
Some exceptions are built into the language
Others can be created and thrown by the programmer
2
Exceptions in Java
Java exception handling
Exceptions are handled using try-catch blocks
try
{ // code that will normally execute
}
catch (ExceptionType1 e)
{ // code to "handle" this exception
}
catch (ExceptionType2 e)
{ // code to "handle" this exception
}
... // can have many catches
finally
{ // code to "clean up" before leaving try block
}
2
Keywords for Exception Handling
throws
Describes the exceptions which can be raised by a method.
throw
Raises an exception to the first available handler in the call stack,
unwinding the stack along the way.
try
Marks the start of a block associated with a set of exception handlers.
catch
If the block enclosed by the try generates an exception of this type,
control moves here; watch out for implicit subsumption.
finally
Always called just before returning back to calling function, irrespective
of exception occurs, or not.
2
Exception classes Hierarchy
java.lang.ThreadDeath
java.lang.Error
java.lang.NullPointerException java.lang.IllegalArgumentException
java.lang.RuntimeException
java.io.FileNotFoundException
java.io.IOException
java.lang.Exception
java.lang.Throwable
2
Exceptions in Java
If all goes well (no exceptions occur)
Code in try block is executed, followed by code in
(optional) finally block
If an exception occurs anywhere in the try block
Execution immediately jumps out of the try block
An exception handler is sought in a catch block
If exception is handled in a catch block, that block
executes; if not, exception is propagated
Whether exception is handled or propagated, finally
block is executed
2
Exceptions in Java
If an exception is handled
Execution resumes immediately AFTER try/catch block in which it was
handled, and does NOT return to throw point
termination model of exception handling
As opposed to a resumption model, where execution resumes from where the
exception occurred
If an exception is propagated
A handler is searched for by backing up through the call chain on the run-
time stack
This is dynamic exception propagation
If no handler is ever found
Console applications crash and report exception
GUI applications will continue to execute, but may be in an inconsistent
state.
2
Exceptions in Java
Checked vs. Unchecked exceptions
Checked exceptions
If a method does NOT handle these, the method MUST
state that it throws them
Done in a throws clause in the method header
These include IOException, and InterruptedException
(and their subclasses)
Unchecked exceptions
Method not required to explicitly have throws for it.
These include RunTimeException and Error
2
Exceptions in Java
Catching exceptions
Catching a super class of an exception will catch
subclass exception objects
catch (Exception e)
"catch all" if no other exceptions match
Should list exceptions in order of most specific
to most general
If catch above is first NO OTHER catches in the block
could ever execute
It is better style to be as specific as possible with
the exceptions that are caught.
2
Steps of trycatchfinally
Every try block must have at least one catch or finally
block attached.
If an exception is raised during a try block:
The rest of the code in the try block is skipped over.
If there is a catch block of the correct, or derived, type in this
stack frame it is entered.
If there is a finally block, it is entered.
If there is no such block, the JVM moves up one stack frame.
If no exception is raised during a try block, and there is
no System.exit() statement:
If there is a matching finally block it is entered.
2
Creating a New Exception Class
A programmer defined exception is an object of
class Exception or one of its subclasses.
Typical use would be:
if (stack == null) throw new StackUnderflowException();
2
Throwing an Exception
Example
class InvalidAgeException extends Exception{
InvalidAgeException(String s){
super(s);
}
}
5
/
2
9
/
2
0
1
4
class Excep13{
static void validate(int age)throws InvalidAgeException{
if(age<18)
throw new InvalidAgeException("not valid");
else
System.out.println("welcome to vote");
}
public static void main(String args[]){
try{
validate(13);
}catch(Exception m){System.out.println("Exception occured: "+m);}
System.out.println("rest of the code...");
}
}
Thank You