Exception Handling In Java

18,647 views
18,476 views

Published on

These slides explain how Java supports Exception handling

Published in: Technology, News & Politics
1 Comment
11 Likes
Statistics
Notes

  • sir I forget my login name or password please tell me
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
18,647
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
785
Comments
1
Likes
11
Embeds 0
No embeds

No notes for slide

Exception Handling In Java

  1. 1. Exception Handling In Java <ul><ul><li>Parag Shah </li></ul></ul><ul><ul><li>Adaptive Software Solutions </li></ul></ul>
  2. 2. Introduction <ul><li>Why is Exception Handling important </li></ul><ul><li>How errors and exceptional conditions were handled in C. </li></ul><ul><li>Approach to Exception handling in Java </li></ul>
  3. 3. Exception Classes <ul><li>Exceptions are objects </li></ul>
  4. 4. Exceptions – Under The Hoods <ul><li>What happens when an Exception is thrown </li></ul>
  5. 5. Dealing With an Exception <ul><li>Catching the Exception </li></ul><ul><ul><li>The try - catch block </li></ul></ul><ul><ul><ul><ul><li>Single catch (see SimpleException.java) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Multiple catch (see SimpleExceptions.java) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Using both parent & subclass in catch clause </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Always catch the appropriate Exception </li></ul></ul></ul></ul><ul><ul><li>Termination vs Resumption of program control </li></ul></ul><ul><li>Propagating the Exception (see ThrowingAndPropagating.java) </li></ul><ul><li>Exception chaining (see ExceptionChaining.java) </li></ul>
  6. 6. Throwing Exceptions <ul><li>Creating and throwing an Exception </li></ul><ul><li>The throws clause </li></ul><ul><li>Choosing the Exception to throw </li></ul>
  7. 7. Creating Custom Exceptions (see MyException.java) <ul><li>When do we need to create custom Exceptions </li></ul><ul><li>Extend an Exception class </li></ul><ul><ul><ul><li>Extend Exception </li></ul></ul></ul><ul><ul><ul><li>Extend a subclass of Exception </li></ul></ul></ul><ul><li>Override constructors </li></ul><ul><li>The name says it all </li></ul><ul><li>Approaches to Exception creation </li></ul><ul><ul><ul><li>Using a generic Exception with different messages </li></ul></ul></ul><ul><ul><ul><li>Using different Exception classes </li></ul></ul></ul><ul><ul><ul><li>Adding additional objects to the Exception </li></ul></ul></ul>
  8. 8. Exception Specification <ul><li>Methods which throw an Exception must define them using the throws clause </li></ul><ul><li>Runtime Exceptions need not be defined using the throws clause </li></ul><ul><li>java.lang.Exception API </li></ul><ul><li>Standard Java Exceptions </li></ul><ul><li>Using Exception in overriden methods (will not allow) </li></ul>
  9. 9. Runtime Exceptions (see RuntimeExceptionExample.java) <ul><li>Runtime Exceptions are special </li></ul><ul><li>Runtime Exceptions in Java </li></ul><ul><li>When to use Runtime Exceptions </li></ul>
  10. 10. The finally clause (see Finally.java) <ul><li>The finally is used to perform cleanup after an Exception is thrown </li></ul><ul><li>When is finally called </li></ul><ul><li>The lost Exception </li></ul><ul><li>Finally without a catch </li></ul>
  11. 11. Best Practices with Exceptions <ul><li>Do not catch an Exception if you cannot deal with it </li></ul><ul><li>Always use finally to perform clean up activities </li></ul><ul><li>Name custom Exceptions appropriately </li></ul><ul><li>Add appropriate messages to Exceptions </li></ul><ul><li>Do not throw too many Exceptions from a method </li></ul><ul><li>Use Wrappers where necessary </li></ul>
  12. 12. Summary <ul><li>Exceptions are used for improved error recovery </li></ul><ul><li>Using Exceptions appropriately will result in robust programs </li></ul><ul><li>Revisit Exception API </li></ul>
  13. 13. Where to Get More Information <ul><li>http://www.adaptivelearningonline.net/adaptivecourses/2006/7/28/exception-handling.html </li></ul><ul><li>Thinking In Java – chapter on “Error Handling With Exceptions” </li></ul><ul><li>http://java.sun.com/docs/books/tutorial/essential/exceptions/ </li></ul><ul><li>Google: Java exception best best practices </li></ul>

×