23_exceptions - CSCI-1200 Data Structures - Fall 2010...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSCI-1200 Data Structures — Fall 2010 Lecture 23 — C++ Exceptions Review from Lecture 22 Basic mechanisms of inheritance Types of inheritance Is-A, Has-A, As-A relationships among classes. Polymorphism Today’s Class Error handling strategies Basic exception mechanisms: try / throw / catch STL exception s RAII “Resource Allocations is Initialization” Structured Exception Handling in the Windows Operating System Google’s C++ Style Guide on Exceptions Some examples from today’s lecture are drawn from: http://www.cplusplus.com/doc/tutorial/exceptions/ http://www.parashift.com/c++-faq-lite/exceptions.html 23.1 Error Handling Strategy A: Denial Assume there are no errors. Command line arguments will always be proper, any specified files will always be available for read/write, the data in the files will be formatted correctly, numerical calculations will not attempt to divide by zero, etc. double answer = numer / denom; For small programs, for short term use, by a single programmer, where the input is well known and controlled, this may not be a disaster (and is often fastest to develop). But for large programs, this code will be challenging to maintain. It can be difficult to pinpoint the source of an error. The symptom of a problem (if noticed at all) may be many steps removed from the source. The software system maintainer must be familiar with the assumptions of the code (which is difficult if the code was written some time ago, by someone else, or is not sufficiently commented. .. or all of the above!). 23.2 Error Handling Strategy B: Plan for the Worst Case Anticipate every mistake or source of error (or as many as you can think of). Write lots of if statements everywhere there may be a problem . Write code for what to do instead, print out error messages, and/or exit when nothing seems reasonable. // for some application specific epsilon (often not easy to specify) double epsilon = 0.00001; if (fabs(denom) < epsilon) { std::cerr << "detected a divide by zero error" << std::endl; // what to do now? (often there is no "right" thing to do) answer = 0; } else { answer = numer / denom; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The code gets bulkier and harder to understand/maintain. If a nested function call might have a problem, and the error needs to be propagated back up to a function much earlier on the call stack, all the functions in between must also test for the error condition and pass the error along. (This is messy to code and all that error checking has performance implications).
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/21/2012 for the course CSCI 1200 taught by Professor Cutler during the Fall '08 term at Rensselaer Polytechnic Institute.

Page1 / 5

23_exceptions - CSCI-1200 Data Structures - Fall 2010...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online