1 / 12

Designing with Java Exception Handling

Designing with Java Exception Handling. B.Ramamurthy. Exception Handling. When a condition arises that the currently executing code cannot handle an exception occurs. Such conditions require special exception handling facilities.

rossv
Download Presentation

Designing with Java Exception Handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Designing with Java Exception Handling B.Ramamurthy B.Ramamurthy

  2. Exception Handling • When a condition arises that the currently executing code cannot handle an exception occurs. • Such conditions require special exception handling facilities. • Traditionally these were handled by function “return” value. (This does not satisfy any of the requirements stated next.) B.Ramamurthy

  3. Requirements • Separation between between normal (regular) code and exception handlers. • Clear representation • Synchronization : source and target, exception propagation • Enforcement • Java offers a superior Exception handling model which is very simple to use and easy to implement in any large application. • Besides the above features its exception model scales very well and offers a uniform interface (for ease of use). B.Ramamurthy

  4. Basics of Java Exception Handling • Use try, throw, catch primitives. • Enclose the code that you expect to result in an exception within a try block: • Attach a catch block that contains the code to handle the exception, following the try block. Use throw to transfer control to catch an exception (thrown). • There can be many catch blocks following a single try to process the various types of exceptions. • “throws” keyword is used to indicate which methods have the potential to throw an exception. When calling these methods the calls have to be enclosed within try blocks. B.Ramamurthy

  5. Try blocks • Syntax: try { normal statements; statements that may result in exceptions; //dynamic scope “throw” happens from here } B.Ramamurthy

  6. Throwing an Exception • throw is a keyword that can be used to announce that an exception has occurred. • Throw normally specifies one operand. • Thrown operand is an Object : an object of Exception class is thrown. • When an exception is thrown, control exits the try block and proceeds to the appropriate catch handler after the try block. B.Ramamurthy

  7. Catching an Exception • Exception handlers are located within catch blocks. • Each catch block starts with the keyword catch followed by parentheses containing a type of the exception this catch can handle. • This is followed by the code for handling the exception enclosed within curly brackets. B.Ramamurthy

  8. Exception handling : examples • For distributed computing : Ex: waiting for other host to respond. • When dynamically allocating memory (large chunks). • In large projects to handle error processing in a uniform manner project-wide. • Simple input validation B.Ramamurthy

  9. Java Exception class • java.lang.Exception class : public class Exception extends Throwable { public Exception() { super();} public Exception(String s){super(s);} } B.Ramamurthy

  10. User-defined Exception class • For every project you implement you need to have a application dependent exception classes so that objects of this type can be thrown. • Java API also supports an extensive list of Exceptions. B.Ramamurthy

  11. Problem Solving • Build a Automatic Teller machine with simple Deposit, Withdraw, and Balance features and withdraw and deposit operation. • User defined “InsufficientFundsException” and system’s “NumberFormatException” are handled, and use try, catch and throw for exception prone code. • See code: B.Ramamurthy

  12. Summary • We discussed and implement a robust system with Exception handling facility. • The system we designed shows how simple it is to model even such abstract concepts as exceptions into classes and objects and to make your systems robust. B.Ramamurthy

More Related