CMSC 202 Exceptions Dealing with errors Most commonly

  • Slides: 14
Download presentation
CMSC 202 Exceptions

CMSC 202 Exceptions

Dealing with errors • Most commonly, error-handling code is interspersed throughout the system’s code.

Dealing with errors • Most commonly, error-handling code is interspersed throughout the system’s code. – Advantage – error processing is in the immediate vicinity of the code that caused the error – Disadvantage – system code becomes “polluted” with error processing and the code becomes difficult to read and understand 2

Common Errors • • • Memory allocation error when using “new” File open error

Common Errors • • • Memory allocation error when using “new” File open error Out of bounds subscript array Division by zero Invalid function parameters 3

Error Handling Techniques • assert (condition) – if the condition is false, the program

Error Handling Techniques • assert (condition) – if the condition is false, the program terminates • Ignore the error – devastating for “real” products, but maybe okay for your own software • Set an indicator for other code to detect • Issue an error message and exit 4

C++ Exception Handling • Removes error handling code from the “main” code • Possible

C++ Exception Handling • Removes error handling code from the “main” code • Possible to catch all kinds of exceptions, exceptions of a certain type, or related types • Used in situations in which the system can recover – this recovery procedure is called an “exception handler”. • Use exception handling when the error will be dealt with by a different part of the program (i. e. different scope) from which that detected the error 5

Error handling can be “slow” • Errors occur infrequently, so there is no need

Error handling can be “slow” • Errors occur infrequently, so there is no need for them to be implemented with efficiency in mind • Therefore, do not use exception handling techniques as alternative program control 6

When to use exception handling • Exceptional situations • To process exceptions from program

When to use exception handling • Exceptional situations • To process exceptions from program components (libraries, functions, classes) that are widely used and where it does not make sense for them to handle their own errors (exceptions handled in different scope) • To allow uniform error processing in a large project 7

C++ exception basics • C++ exception handling is geared to situations in which the

C++ exception basics • C++ exception handling is geared to situations in which the function that detects the error can’t handle it (e. g. divide by zero). • Such a function will throw an exception • There is no guarantee that there will an exception handler geared toward processing any particular kind of exception. If not, the program terminates. 8

What gets thrown • The operand of throw can be of any type. •

What gets thrown • The operand of throw can be of any type. • If the operand is an object, we call it an “exception object”. class Div. By. Zero. Ex { public: Div. By. Zero. Ex ( ) : message (“attempted divide by 0”) { } const char *msg const { return message; } private: const char *message; } 9

try, throw, catch • Code which may throw an exception is enclosed in a

try, throw, catch • Code which may throw an exception is enclosed in a try block. • The try block is followed by one or more catch blocks. Each catch block specifies the type of exception it can catch and handle. If the exception matches the type of parameter in one of the catch blocks, the code for that catch block is executed. • If no exception handler is found the program terminates. • If no exception is thrown, control passes to the first statement after the last catch block following the try. 10

throwing • A temporary copy of the throw operand is created and initialized. •

throwing • A temporary copy of the throw operand is created and initialized. • Control exits the try block (all automatic objects defined within the try block are destroyed) • Control proceeds to the appropriate catch block (if any). • The copy of the operand initializes the catch block parameter 11

Which catch block? • Each catch block defines its own scope • The first

Which catch block? • Each catch block defines its own scope • The first catch block listed that matches the type of the thrown object gets executed • catch (. . . ) matches all exceptions – so put it last – but you can’t tell what type of exception you caught. • After the catch block completes, control passes to the first statement after the last catch block for this try. 12

How to catch • Not with a pointer – Might point to a destroyed

How to catch • Not with a pointer – Might point to a destroyed object – If not, who will delete it? • Not by value – Makes a 2 nd copy – Other problems related to derived classes • Always catch exceptions by reference 13

Nested function calls and stack unwinding • When an exception is thrown but not

Nested function calls and stack unwinding • When an exception is thrown but not caught in the current scope, the function-call stack is unwound an attempt is made to catch the exception in the next outer try/catch block. • Unwinding means that all local variables in that function are destroyed and control returns to the point the function was called. 14