Exception handling in java

727 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
727
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
61
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Exception handling in java

  1. 1. Exception Handling In Java By Priyanka zope
  2. 2. Content 1:Introduction. 2:Error and Error Handling. 3:Exception. 4:Type Of Exception. 5:Coding Exception. 6:Advantages
  3. 3. Error and Error Handling. v An Error is any unexpected result obtained from a program during execution. v Unhandled errors may manifest themselves as incorrect results or behavior, or as abnormal program termination. v Errors should be handled by the programmer, to prevent them from reaching the user.
  4. 4. Exception Exception- a better error handling. Exception – an error condition that can occur during the course of a program execution Ø In Java, exceptions are objects themselves Exception handling is another form of control structure (like ifs and switch statements) Ø When an error is encountered, the normal flow of the program is stopped and the exception is Handled.
  5. 5. Types Of Exception I:Checked Exception Checked at compile time. II:Unchecked Exception Checked at runtime.
  6. 6. Coding Exception 1: try…..catch block 2:finally 3: throw
  7. 7. Try…catch block • To process an exception when it occurs, the line that throws the exception is executed within a try block. • A try block is followed by one or more catch clauses, which contain code to process an exception. • The catch statement is used for catching exceptions. try { <try block> } catch ( <ExceptionClass> <name> ) { <catch block> } catch ( <ExceptionClass> <name> ) { <catch block>}
  8. 8. Finally clause • A try statement can have an optional clause designated by the reserved word finally. • If no exception is generated, the statements in the finally clause are executed after the statements in the try block complete. try { // statements that throw exceptions } catch(<exception>) { // do stuff } finally { – // code here runs whether or not catch runs }
  9. 9. Throw All methods use the throw statement to throw an exception. The throw statement requires a single argument: a throwable object. Throwable objects are instances of any subclass of the Throwable class. Here's an example of a throw statement.
  10. 10. Problem IllegalArgumentException: illegal parameter value Problem public class BankAccount { public void withdraw(double amount) { if (amount > balance) { ????????? } balance = balance - amount; } . . .}
  11. 11. Solution public class BankAccount { public void withdraw(double amount) { if (amount > balance) { IllegalArgumentException exception = new IllegalArgumentException("Amount exceeds balance"); throw exception; } balance = balance - amount; } . . . }
  12. 12. Advantages 1:Separating Error-Handling Code from "Regular" Code 2:Grouping and Differentiating Error Types 3:consistency 4:flexibility 5:simplicity

×