C++_exceptions - CS 342 C Exceptions Motivation for C...

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

View Full Document Right Arrow Icon
Copyright © 2004 Dept. of Computer Science and Engineering, Washington University CS 342: C++ Exceptions Motivation for C++ Exceptions void Number::operator/=(const double denom) { if (denom == 0.0) { // what do we do here? } m_value /= denom; } Need to handle divide by zero case Otherwise bad things happen Program crash Wrong results Could set value to NaN Special “not-a-number” value But, caller might fail to check for it Could return a “special” value Might be a valid return value Could still be ignored Exceptions offer a better alternative
Image of page 1

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

View Full Document Right Arrow Icon
Copyright © 2004 Dept. of Computer Science and Engineering, Washington University CS 342: C++ Exceptions C++ Expression Syntax void foo() throw (int) { throw 2; } catch (int &i) { cout << i << endl; } catch (...) { cout << “another exception” << endl; } Can throw any class Specify what a function throws in it’s declaration Can catch and use exceptions “Default” catch block
Image of page 2