120 likes | 133 Views
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.
E N D
Designing with Java Exception Handling B.Ramamurthy 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. • Traditionally these were handled by function “return” value. (This does not satisfy any of the requirements stated next.) B.Ramamurthy
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
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
Try blocks • Syntax: try { normal statements; statements that may result in exceptions; //dynamic scope “throw” happens from here } B.Ramamurthy
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
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
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
Java Exception class • java.lang.Exception class : public class Exception extends Throwable { public Exception() { super();} public Exception(String s){super(s);} } B.Ramamurthy
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
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
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