Exception Handling - PowerPoint PPT Presentation

Loading...

PPT – Exception Handling PowerPoint presentation | free to download - id: 18075e-ZDc1Z



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Exception Handling

Description:

Typically error-handling code is put where errors are expected to occur. Advantage ... IBM PC and compatibles: ctrl z. Macintosh: ctrl d. Throwing an Exception ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 24
Provided by: williama3
Learn more at: http://www2.hawaii.edu
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Exception Handling


1
Exception Handling
  • Introduction
  • Try
  • Throw
  • Catch

2
Exception Handling
  • Use exception handling to write programs that are
  • Clearer
  • More robust
  • More fault-tolerant
  • Exception examples
  • Failure of new to obtain enough memory
  • An out-of-bounds array subscript
  • Arithmetic overflow
  • Division by zero
  • Invalid function parameters

3
Error-handling Code
  • Typically error-handling code is put where errors
    are expected to occur
  • Advantage
  • Programmers can easily check to if error handling
    has been done in the immediate vicinity of the
    code
  • Disadvantage
  • Code may become cluttered with too much error
    processing

4
Cs Exception Handling
  • Using Cs exception handling removes most of
    the error handling code from the main program
  • Improves program readability and modifiability
  • Can handle synchronous errors
  • Such as division by zero
  • Cannot handle asynchronous situations
  • Such as network message arrivals
  • Mouse clicks

5
Cs Exception Handling
  • Exception handling is used to
  • Allow the system to recover from an exception
  • Or (in the case of an unrecoverable error) clean
    up the mess shut down gracefully

6
The Basics try, throw, catch
  • Code that may generate an exception is encased in
    a try block
  • Within the try bock an exception is thrown
  • After the try block, a catch block will catch and
    handle the exception, so that the program can
    return to its normal state
  • Program will search for a catch block with the
    same parameter type as the exception type
  • If an exception is not handled, the program will
    terminate

7
Exception Class Function
  • class DBZException
  • public
  • DBZException()
  • message("ERROR division by zero")
  • void print() const coutltltmessageltltendl
  • private
  • const char message
  • double quotient( int num, int den )
  • if ( den 0 )
  • throw DBZException()
  • return static_castltdoublegt(num)/den
  • (See DBZException.cpp.txt)

8
Main
  • int main()
  • int num1, num2
  • double result
  • cout ltlt "Enter two integers (EOF to end)
    "
  • while ( cin gtgt num1 gtgt num2 )
  • try
  • result quotient( num1, num2 )
  • coutltlt"The quotient is
    "ltltresultltltendl
  • catch ( DBZException ex )
  • ex.print()
  • coutltlt"\nEnter two integers (EOF to end)
    "
  • return 0

9
Output
  • Even thought the user made a mistake, the program
    can continue without an abend (ABnormal END)
  • Enter two integers (EOF to end) 3 4
  • The quotient is 0.75
  • Enter two integers (EOF to end) 4 0
  • ERROR division by zero
  • Enter two integers (EOF to end) 4 5
  • The quotient is 0.8

10
End-of-File Marker
  • File ends with a end-of-file (EOF) marker
  • EOF key combinations on different systems
  • UNIX systems ltctrlgtd
  • IBM PC and compatibles ltctrlgtz
  • Macintosh ltctrlgtd

11
Throwing an Exception
  • A throw indicates that an exception occurred
  • The throw operand can be of any type
  • Can throw objects not intended for error handling
  • When an operand is thrown
  • A temporary copy is made initialized
  • When this operand is caught
  • It initializes exception handlers parameter
  • When the exception handler completes execution
    and exits, the temporary object is destroyed

12
Throwing an Exception II
  • If a handler is not found within a try block
  • The program will search for a matching handler in
    the next nested try block, until it is found
  • All exceptions thrown outside a try block will
    terminate the program
  • All objects declared in a try block will be
    destroyed before an exception is thrown from that
    block

13
New Exception Class
  • class DBZException
  • public
  • DBZException()message("ERROR division by
    zero")
  • void print() const coutltltmessageltltendl
  • private const char message
  • class NIFException
  • public
  • NIFException()
  • message("ERROR numerator is five")
  • void print() const
  • coutltltmessageltltendl
  • private
  • const char message

14
Functions
  • void checkNumeratorIs5(int n)
  • if(n5) throw NIFException()
  • if(n100) throw int()
  • double quotient( int num, int den )
  • if ( den 0 )
  • throw DBZException()
  • try
  • checkNumeratorIs5(num)
  • coutltlt"No exception in
    QUOTIENT()"ltltendl
  • catch(int e)
  • coutltlt"Type INT exception
    thrown"ltltendl
  • return static_castlt double gt ( num ) / den

15
Main
  • int main()
  • int num1, num2
  • double result
  • cout ltlt "Enter two integers (EOF to end) "
  • while ( cin gtgt num1 gtgt num2 )
  • try
  • result quotient( num1, num2 )
  • coutltlt"The quotient
    is"ltltresultltltendl
  • catch ( DBZException e )
  • e.print()
  • catch ( NIFException e )
  • e.print()
  • coutltlt"\nEnter two integers (EOF to end)
    "
  • return 0

16
Output
  • Enter two integers (EOF to end) 1 2
  • No exception in QUOTIENT()
  • The quotient is 0.5
  • Enter two integers (EOF to end) 5 2
  • ERROR numerator is five
  • Enter two integers (EOF to end) 100 4
  • Type INT exception thrown
  • The quotient is 25
  • (See throwing.cpp.txt)

17
Catching an Exception
  • The catch block is used to
  • Catch an exception
  • Execute the code within the block
  • The parameter in a catch statement can be named
    or unnamed
  • The handler catch() will catch any exception
  • If no matching parameter type is found for a
    thrown object, the program terminates

18
Catching an Exception II
  • A catch block can throw another kind of exception
    or re-throw itself
  • These exceptions will not be caught by any catch
    blocks listed after the current catch block
  • They will be caught after the next outer try
    block
  • If not caught, they will terminate the program

19
Exception Class
  • class DBZException
  • public
  • DBZException()
  • message("ERROR division by zero")
  • void print() const coutltltmessageltltendl
  • private
  • const char message
  • (See catching.cpp.txt)

20
Functions
  • void check(int n)
  • if(n100) throw int()
  • double quotient( int num, int den )
  • if ( den 0 )
  • throw DBZException()
  • try
  • check(num)
  • catch( int ) //unnamed parameter
  • coutltlt"Type INT exception thrown"ltltendl
  • throw //re-throw exception
  • return static_castlt double gt ( num ) / den

21
  • void main()
  • int num1, num2
  • double result
  • cout ltlt "Enter two integers (EOF to end) "
  • try
  • while ( cin gtgt num1 gtgt num2 )
  • try
  • result quotient( num1, num2 )
  • coutltlt"The quotient
    is"ltltresultltltendl
  • catch ( DBZException e )
  • e.print()
  • throw float()
  • catch (...)
  • coutltlt"UNDEFINED ERROR I"ltltendl
  • coutltlt"\nEnter two integers "
  • catch(...)
  • coutltlt"UNDEFINED ERROR II"ltltendl

22
Output
  • Enter two integers (EOF to end) 100 25
  • Type INT exception thrown
  • UNDEFINED ERROR I
  • Enter two integers (EOF to end) 100 0
  • ERROR division by zero
  • UNDEFINED ERROR II
  • (program terminates)

23
Class Exercise 1
  • See exercise1.txt
About PowerShow.com