Exception%20Handling%20Fall%202008 - PowerPoint PPT Presentation

About This Presentation
Title:

Exception%20Handling%20Fall%202008

Description:

Most exceptions are infrequent or should not happen at all. ... Harkins back to the day of the ' go to ' when the execution path of computer ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 11
Provided by: drdavida
Learn more at: http://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Exception%20Handling%20Fall%202008


1
Exception HandlingFall 2008
  • Dr. David A. Gaitros
  • dgaitros_at_admin.fsu.edu

2
Exception Handling
  • A well behaved program never abnormally
    terminates. All errors or problems should be
    handled by the program
  • Exception A problem that occurs that is outside
    the normal expected behavior of the system.
    Example Divide by zero

3
Exception Handling
  • Most exceptions are infrequent or should not
    happen at all.
  • The code for the exception should be separated
    from the main body to avoid confusing maintenance
    activities.
  • Harkins back to the day of the go to when the
    execution path of computer programs was less well
    behaved.

4
Exception Handling
  • Exception handling best used for situations that
    would normally cause the program to abnormally
    terminate or produce unpredictable results
  • Divide by Zero
  • Array subscript out of bounds
  • Register or number overflow or underflow
  • Stack overflow or underflow
  • Abnormal operation
  • Parity error

5
Simple Example
  • include ltiostreamgt
  • using namespace std
  • int main()
  • int cookies, people
  • double cpp
  • try
  • cout ltlt "Enter number of people "
  • cin gtgt people
  • cout ltlt "Enter number of cookies "
  • cin gtgt cookies
  • if (cookies 0) throw people
  • else if (cookies lt 0)
  • throw static_castltdoublegt(people)
  • cpp okies/static_castltdoublegt(people)
  • cout ltlt cookies ltlt " cookies.\n"
  • ltlt people ltlt " people.\n"
  • ltlt "You have " ltlt cpp
  • ltlt " cookies per person.\n"

6
  • catch(int e)
  • cout ltlt e ltlt " people, and no cookies!\nGo buy
    some cookies!\n"
  • catch(double t)
  • cout ltlt "Second catch block type double -- do we
    reach it?\n"
  • cout ltlt "End of program.\n" return 0

7
try Block
  • The basic handling of an exception consists of
    the try-throw-catch trio.
  • Basic try block
  • try
  • // Some code goes here
  • // Possibly throw an exception
  • // More regular code can go here.

8
try Block
  • The try block is a method of grouping code
    together that could be associated with one or
    more exceptions and note affecting the rest of
    the program.
  • If an exception is detected, the alternative code
    is executed while the execution of the normal
    code within the try block is halted.
  • The code after the try block is not affected.
  • If an exception occurs within a try block, the
    try block ends and the program attempts to match
    the exception with one of the catch handlers.
  • If match found, code in catch is executed
  • Only one catch block will be executed
  • Execution resumes after last catch block

9
Example try-catch
  • try
  • if(Array_Index gt 100)
  • throw Array_Index
  • catch ( int e)
  • cout ltlt eltlt is greater than 100ltlt
  • ltlt resetting index to 100\n
  • Array_Index 100

10
Overuse of Exceptions
  • Exceptions alter flow of control
  • Similar to old "goto" construct
  • "Unrestricted" flow of control
  • Should be used sparingly
  • Good rule
  • If desire a "throw" consider how to write
    program without throw
  • Ask yourself how often you expect to encounter
    this error
  • Errors caused by bad user input is usually not a
    reason to write an exception to handle it
  • If alternative reasonable ? do it
Write a Comment
User Comments (0)
About PowerShow.com