Вы находитесь на странице: 1из 13

Exception Handling In Java

Parag Shah
Adaptive Software Solutions

1
Adaptive Software Solutions: http://www.adaptivesoftware.biz
Introduction

 Why is Exception Handling important
 How errors and exceptional conditions
 were handled in C.
 Approach to Exception handling in Java

2
Adaptive Software Solutions: http://www.adaptivesoftware.biz
Exception Classes
 Exceptions are objects

3
Adaptive Software Solutions: http://www.adaptivesoftware.biz
Exceptions – Under The Hoods
 What happens when an Exception is
thrown

4
Adaptive Software Solutions: http://www.adaptivesoftware.biz
Dealing With an Exception

 Catching the Exception
 – The try - catch block
 – Single catch (see SimpleException.java)
 – Multiple catch (see SimpleExceptions.java)
 – Using both parent & subclass in catch clause
 – Always catch the appropriate Exception
– Termination vs Resumption of program
control

 Propagating the Exception (see ThrowingAndPropagating.java)
 Exception chaining (see ExceptionChaining.java)
5
Adaptive Software Solutions: http://www.adaptivesoftware.biz
Throwing Exceptions

 Creating and throwing an Exception

 The throws clause
 Choosing the Exception to throw

6
Adaptive Software Solutions: http://www.adaptivesoftware.biz
Creating Custom Exceptions
(see MyException.java)

 When do we need to create custom Exceptions



 Extend an Exception class
 Extend Exception

 Extend a subclass of Exception

 Override constructors
 The name says it all

 Approaches to Exception creation
  Using a generic Exception with different messages
  Using different Exception classes
 Adding additional objects to the Exception

7
Adaptive Software Solutions: http://www.adaptivesoftware.biz
Exception Specification
Methods which throw an Exception must
 define them using the throws clause
 Runtime Exceptions need not be defined
 using the throws clause
 java.lang.Exception API

 Standard Java Exceptions

 Using Exception in overriden methods
(will not allow)
8
Adaptive Software Solutions: http://www.adaptivesoftware.biz
Runtime Exceptions
(see RuntimeExceptionExample.java)


 Runtime Exceptions are special

 Runtime Exceptions in Java
 When to use Runtime Exceptions

9
Adaptive Software Solutions: http://www.adaptivesoftware.biz
The finally clause
(see Finally.java)

The finally is used to perform cleanup


 after an Exception is thrown
 When is finally called

 The lost Exception

 Finally without a catch

10
Adaptive Software Solutions: http://www.adaptivesoftware.biz
Best Practices with Exceptions
 Do not catch an Exception if you cannot deal with it

 Always use finally to perform clean up activities

 Name custom Exceptions appropriately

 Add appropriate messages to Exceptions

 Do not throw too many Exceptions from a method
 Use Wrappers where necessary

11
Adaptive Software Solutions: http://www.adaptivesoftware.biz
Summary
Exceptions are used for improved error
 recovery
 Using Exceptions appropriately will result
 in robust programs
 Revisit Exception API

12
Adaptive Software Solutions: http://www.adaptivesoftware.biz
Where to Get More Information
http://www.adaptivelearningonline.net/adaptivecourses/2006/7/28/exception-

 Thinking In Java – chapter on “Error Handling With Exceptions”
http://java.sun.com/docs/books/tutorial/essential/exceptions/
 Google: Java exception best best practices 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 
 13
Adaptive Software Solutions: http://www.adaptivesoftware.biz

Вам также может понравиться