{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right Arrow Icon
CSE 332: C++ Exceptions C++ Exception Syntax void foo() throw (int) { throw 2; } try { foo(); } catch (int &i) { 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
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
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern