KEMBAR78
Exception Handling In Java | ODP
Exception Handling In Java Parag Shah Adaptive Software Solutions
Introduction  Why is Exception Handling important How errors and exceptional conditions were handled in C. Approach to Exception handling in Java
Exception Classes Exceptions are objects
Exceptions – Under The Hoods What happens when an Exception is thrown
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)
Throwing Exceptions Creating and throwing an Exception The  throws  clause Choosing the Exception to throw
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
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)
Runtime Exceptions  (see RuntimeExceptionExample.java) Runtime Exceptions are special Runtime Exceptions in Java When to use Runtime Exceptions
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
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
Summary Exceptions are used for improved error recovery Using Exceptions appropriately will result in robust programs Revisit Exception API
Where to Get More Information http://www.adaptivelearningonline.net/adaptivecourses/2006/7/28/exception-handling.html Thinking In Java – chapter on “Error Handling With Exceptions” http://java.sun.com/docs/books/tutorial/essential/exceptions/ Google: Java exception best best practices

Exception Handling In Java

  • 1.
    Exception Handling InJava Parag Shah Adaptive Software Solutions
  • 2.
    Introduction Whyis Exception Handling important How errors and exceptional conditions were handled in C. Approach to Exception handling in Java
  • 3.
  • 4.
    Exceptions – UnderThe Hoods What happens when an Exception is thrown
  • 5.
    Dealing With anException 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)
  • 6.
    Throwing Exceptions Creatingand throwing an Exception The throws clause Choosing the Exception to throw
  • 7.
    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
  • 8.
    Exception Specification Methodswhich 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)
  • 9.
    Runtime Exceptions (see RuntimeExceptionExample.java) Runtime Exceptions are special Runtime Exceptions in Java When to use Runtime Exceptions
  • 10.
    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
  • 11.
    Best Practices withExceptions 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
  • 12.
    Summary Exceptions areused for improved error recovery Using Exceptions appropriately will result in robust programs Revisit Exception API
  • 13.
    Where to GetMore Information http://www.adaptivelearningonline.net/adaptivecourses/2006/7/28/exception-handling.html Thinking In Java – chapter on “Error Handling With Exceptions” http://java.sun.com/docs/books/tutorial/essential/exceptions/ Google: Java exception best best practices