1 / 20

Exception Handling

Exception Handling. Reading: Sections13.1 - 13.9, 13.11 only. Dealing With Errors. Most commonly, error-handling code is interspersed throughout a system’s code. Advantage – error processing is in the immediate vicinity of the code that caused the error (easy to find) Disadvantages

sandyl
Download Presentation

Exception Handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exception Handling Reading: Sections13.1 - 13.9, 13.11 only

  2. Dealing With Errors • Most commonly, error-handling code is interspersed throughout a system’s code. • Advantage – error processing is in the immediate vicinity of the code that caused the error (easy to find) • Disadvantages • code becomes “polluted” with error processing and the code becomes difficult to read and understand • application code does not get to handle errors in the way that it considers most appropriate

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

  4. 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 (e.g., return a flag) • Issue an error message and exit

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

  6. Error Handling Can Be Slow • Error handling can slow a program down. • But, errors occur infrequently, so there is no need for them to be implemented with efficiency in mind. • However, do not use exception handling techniques as an alternative to proper program control.

  7. 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 • To allow uniform error processing in a large project

  8. C++ Exception Basics - throw • C++ exception handling is geared to situations in which the function that detects the error can’t or shouldn’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.

  9. throw Example double quotient(int num, int den) { if (den == 0) throw DivByZeroEx(); return double(num) / den; }

  10. 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 DivByZeroEx { public: DivByZeroEx ( ) : message (“attempted divide by 0”) { } const char *what const { return message;} private: const char *message;}

  11. C++ Exception Basics: try and catch • Code that 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 exactly 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.

  12. try and catch Example int main() { int number1, number2; double result; . . . try { result = quotient(num1, num2); cout << “The quotient is: “ << result << endl; } catch (DivByZeroEx ex) { // exception handler cout << “Exception occurred: “ << ex.what() << endl; } . . . }

  13. What Gets Caught • A temporary copy of the throw operand is created and initialized. • Control immediately 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 catchblock parameter, if there is one.

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

  15. Multiple catch’s try { // code that might throw an exception } catch (ExcepObject1 ex1) { // exception handler code } . . . catch (ExcepObjectn exn) { // exception handler code } catch (…) { // default exception handler code }

  16. Nested Function Calls and Stack Unwinding • When an exception is thrown but not caught in the current scope, the function call stack is unwound and an attempt is made to catch the 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.

  17. Stack Unwinding Example void function3( ) { throw int( ); cout << “function3” << endl; } void function2( ) { function3( ); cout << “function2” << endl; } void function1( ) { function 2( ); cout << “function1” << endl; } int main( ) { try { function1( ); } catch (int) { cout << “Exception occurred” << endl; return 0; }

  18. Rethrowing an Exception • When a handler catches an exception and decides it cannot/should not process it, it can rethrow the exception. • A rethrown exception is detected by the next enclosing try block and handled by an associated catch block. • Exceptions can be rethrown back through multiple levels of nesting. • Syntax: throw;

  19. Rethrowing an Exception (con’t) More syntax: try { // code that could throw an exception } catch { throw; // rethrow the exception to the next } // enclosing try block

  20. Rethrowing Example Application program // handles exception if full Add item to inventory // rethrows exception if full Insert item in list // rethrows exception if full Is list full? // throws exception if full

More Related