Download presentation
Presentation is loading. Please wait.
1
Designing with Java Exception Handling
B.Ramamurthy 4/7/2019 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.) 4/7/2019 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. Beside the above features its exception model scales very well and offers a uniform interface (for ease of use). 4/7/2019 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. 4/7/2019 B.Ramamurthy
5
Try blocks Syntax: try { normal statements;
statements that may result in exceptions; //dynamic scope “throw” happens from here } 4/7/2019 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. 4/7/2019 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 parentheses. 4/7/2019 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. 4/7/2019 B.Ramamurthy
9
Java Exception class java.lang.Exception class :
public class Exception extends Throwable { public Exception() { super();} public Exception(String s){super(s);} } 4/7/2019 B.Ramamurthy
10
User-defined Exception class
For every project you implement you need to have a project dependent exception class so that objects of this type can be thrown. Java API also supports an extensive list of Exceptions. 4/7/2019 B.Ramamurthy
11
Problem Solving Build a Automatic Teller machine with simple Deposit, Withdraw, and Balance features and withdraw and deposit operation. CRC Card Design Class Design : class header, variables, methods User defined “InsufficientFundsException” and system’s “NumberFormatException” are handled, and use try, catch and throw for exception prone code. 4/7/2019 B.Ramamurthy
12
Summary We discussed and implement a system with robust Exception handling facility. The system we designed shows how easy it is to model even such abstract concepts as exceptions into classes and objects. 4/7/2019 B.Ramamurthy
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.