Designing with Java Exception Handling - PowerPoint PPT Presentation

About This Presentation
Title:

Designing with Java Exception Handling

Description:

'throws' keyword is used to indicate which methods have the ... specifies one operand. ... to handle error processing in a uniform manner project-wide. ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 13
Provided by: kumarm8
Learn more at: https://cse.buffalo.edu
Category:

less

Transcript and Presenter's Notes

Title: Designing with Java Exception Handling


1
Designing with Java Exception Handling
  • 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.)

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).

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.

5
Try blocks
  • Syntax
  • try
  • normal statements
  • statements that may result in exceptions
    //dynamic scope
  • throw happens from here

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.

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.

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

9
Java Exception class
  • java.lang.Exception class
  • public class Exception extends Throwable
  • public Exception() super()
  • public Exception(String s)super(s)

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.

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

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.
Write a Comment
User Comments (0)
About PowerShow.com