Unit 6 - Exception Handling
Unit 6 - Exception Handling
Basic Java
Unit 6 Exception Handling
Basic Java
Topics
Exception Hierarchy
Throwable Class
Error and Exception
Types of Exceptions
Throwing Exceptions
Declaring Exceptions per Method
Catching Exception
Designing user-defined Exception classes
Basic Java
Error Condition
Basic Java
Error Condition
Basic Java
int main () {
int res;
if (can_fail () == -1) {
cout << "Something failed!" << endl;
return 1;
}
if(div(10,0,res) == -1) {
cout << "Division by Zero!" << endl;
return 2;
}
return 0;
}
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Am
IsCan
Ierror
forced
I
understand
to
handling
receive
Is
what
done
my
the
went
code
in a
maintainabl
wrong
series
returning
with
of if
value
then
juste?
block?
from
the a
function?
return
value?
Basic Java
What is an Exception?
Basic Java
Method where
error occurred
Method Call
Looking for
appropriate handler
Main
Basic Java
Am
IsCan
Ierror
forced
I
understand
to
handling
handle
exceptions?
Is
Is
what
done
itmy
easy
went
code
in ato
maintainabl
understand
wrong
series of
and
if
where
then
how
e?
block?
when
to
anhandle
error is
exceptions?
thrown?
Basic Java
Basic Java
Throwable class
Object
Throwable
Error
Exception
RuntimeException
Basic Java
Errors
Basic Java
Types of Exception
Object
Throwable
Error
Exception
RuntimeException
Basic Java
Types of Exception
Checked exceptions
Unchecked or runtime exception
Basic Java
Checked exceptions
Types of Exception
Basic Java
try
catch
finally
Basic Java
try Block
try block
Basic Java
Basic Java
catch Block
try
{
}
catch(ExceptionType name)
{
}
catch(ExceptionType name)
Basic Java
{
}
Basic Java
Basic Java
Basic Java
Exercise
Basic Java
Exception Handler
Clean up code
Basic Java
finally Block
finally{
}
Basic Java
finally {
r.close();
}
Basic Java
System-Defined Exception
Basic Java
IndexOutOfBoundsException :
SecurityException :
NullPointerException :
NegativeArraySizeException :
When beyond the bound of index in the object which use index, such
as array, string, and vector
ArrayStoreException :
System-Defined Exception
IllegalMonitorStateException :
When the thread which is not owner of monitor involves wait or notify
method
Basic Java
Throwing an Exception
Basic Java
Basic Java
}
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
method2();
.
}
Control
userLoginForm()
{
authenticateUserInControl();
}
Handles
Exception with
authenticateUserInControl()
try/catch
{
authenticateUser();
Business Logic
Database
}
authenticateUser() throws Exception
{
Throws
retrieveUserInfo()
exception
}
retrieveUserInfo() throws Exception
{
retrievesData()
{
}
}
Basic Java
String getMessage()
Returns the message that was set while the exception object was
created, by calling the parameterized constructor
Exception(String message)
String toString
void printStackTrace()
Basic Java
StackTrace
Basic Java
If an exception cannot be
represented by those in
the Java platform, a user
can define his own
exception.
The user defined
exception class should
be
a
subclass
of
Exception or any of its
sub types.
public class
ProductNotFoundException
extends Exception
{
}
public Product getProduct(int
prodId) throws
ProductNotFoundException {
Basic Java
Throwing Exceptions
Use the throw clause to throw the user defined exception object
The throw statement requires a throwable object as argument.
Throwable objects are instances of any subclass of the Throwable
class.
throw causes the method to terminate and returns an exception
object to the caller.
public Product getProduct(int prodId) throws
ProductNotFoundException {
// Retrieve product info from the database
// select * from Product where prod_id = prodId
if(!found)
throw new
ProductNotFoundException();
return product;
}
Basic Java
AgeException
OutOfAgeLimitException
TooYoungException
IllegalAgeFormatException
TooOldException
NegativeAgeException
Basic Java
Question time
Please try to limit the questions to the topics discussed during the session. Thank you.
Basic Java