Java exeception handling

306 views
255 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
306
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java exeception handling

  1. 1. Disclaimer:This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocument of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .Ltd
  2. 2. JAVA EXCEPTION HANDLING Ashwin Anand V Email:ashwinanand99@gmail.com Facebook id:ashwinanand99@gmail.com
  3. 3. Exceptions• What are they? – An exception is a representation of an error condition or a situation that is not the expected result of a method. – Exceptions are built into the Java language and are available to all program code.
  4. 4. How are they used? – Exceptions fall into two categories: • Checked Exceptions • Unchecked Exceptions – Checked exceptions are inherited from the core Java class Exception. They represent exceptions that are frequently considered “non fatal” to program execution – Checked exceptions must be handled in your code, or passed to parent classes for handling.
  5. 5. Examples – Checked exceptions include errors such as “array index out of bounds”, “file not found” and “number format conversion”. – Unchecked exceptions include errors such as “null pointer”.
  6. 6. How do you handle exceptions? – Exception handling is accomplished through the “try – catch” mechanism, or by a “throws” clause in the method declaration. – For any code that throws a checked exception, you can decide to handle the exception yourself, or pass the exception “up the chain” (to a parent class). – To handle the exception, you write a “try-catch” block. To pass the exception “up the chain”, you declare a throws clause in your method or class declaration. – If the method contains code that may cause a checked exception, you MUST handle the exception OR pass the exception to the parent class (remember, every class has Object as the ultimate parent)
  7. 7. • Try-Catch Mechanism – Wherever your code may trigger an exception, the normal code logic is placed inside a block of code starting with the “try” keyword: – After the try block, the code to handle the exception should it arise is placed in a block of code starting with the “catch” keyword. – You may also write an optional “finally” block. This block contains code that is ALWAYS executed, either after the “try” block code, or after the “catch” block code. – Finally blocks can be used for operations that must happen no matter what (i.e. cleanup operations such as closing a file)
  8. 8. • Example – try { … normal program code } catch(Exception e) { … exception handling code }
  9. 9. ExampleOutput:Division by zero.After catch statement.
  10. 10. Throw• It is possible for your program to throw an exception explicitly throw TrrowableInstance• Here, TrrowableInstance must be an object of type Throwable or a subclass Throwable
  11. 11. Example Output: Caught inside demoproc. Recaught: java.lang.NullPointerException: demo
  12. 12. Finally• It is used to handle premature execution of a method (i.e. a method open a file upon entry and closes it upon exit)• finally creates a block of code that will be executed after try/catch block has completed and before the code following the try/catch block• finally clause will execute whether or not an exception is thrown
  13. 13. Example
  14. 14. Output inside procA procAs finally Exception caught inside procB procBs finally inside procC procCs finally
  15. 15. • If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance.• www.baabtra.com | www.massbaab.com |ww w.baabte.com
  16. 16. Thank you
  17. 17. Contact Us

×