C++_exceptions - Motivation for C+ Exceptions Void Number:...

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

View Full Document Right Arrow Icon
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value /= denom; } Need to handle cases where program cannot behave normally E.g., zero denominator for division Otherwise bad things happen Program crashes Wrong results Could set value to Number::NaN I.e., a special “not-a-number” value Must avoid using a valid return value… … which can be difficult (e.g., for int) Anyway, caller might fail to check for it Exceptions offer a better alternative
Background image of page 1

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

View Full DocumentRight Arrow Icon
CSE 332: C++ Exceptions C++ Exception Syntax void foo() throw (int) { throw 2; } try { foo(); } cout << “caught ” << i << endl; } catch (. ..) { cout << “another exception” << endl; } Can throw any type A function’s declaration can list types it can throw Otherwise the function may throw any defined type Can catch and inspect or use exceptions “Default” catch block is indicated by three dots Catches every type
Background image of page 2
CSE 332: C++ Exceptions Overview of C++ Exceptions Normal program control flow is halted At the point where an exception is thrown The program call stack “unwinds” Stack frame of each function in call chain “pops” Variables in each popped frame are destroyed This goes on until an enclosing try/catch scope is reached Control passes to first matching catch block Can handle the exception and continue from there Can free some resources and re-throw exception Let’s look at the call stack and how it behaves Good way to explain how exceptions work (in some detail) Also a good way to understand normal function behavior
Background image of page 3

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

View Full DocumentRight Arrow Icon
CSE 332: C++ Exceptions A More Detailed Look at Stack Frames
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/19/2011 for the course CSE 332S taught by Professor Smart during the Spring '08 term at Washington University in St. Louis.

Page1 / 16

C++_exceptions - Motivation for C+ Exceptions Void Number:...

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

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