#include <cstdlib> #include <iostream> #include <assert.h> const int SIZE = 100; class error { public: const string z; error(const string& w) z(w) {} }; class stack { private: int C; int info[SIZE]; public: stack(): C(0) {}; void push(const int item) throw(error); int pop() throw(error); }; inline void stack::push(const int item) throw(error) { if ((C < 0) && (C >= sizeof(info)/sizeof(info[0]))) { throw("Push overflows stack"); } info[C] = item; ++C; } inline int stack::pop( ) throw(error) { --C; if ((C < 0) && (C >= sizeof(info)/sizeof(info[0]))) { throw("Pop underflows stack"); } return (info[C]); } static stack tstack; static void push_s( ) { int i; for (i = 0; i < 5000; i++) { tstack.push(i); } } int main() { try { push_s(); } catch (error& err) { cerr << "Error: Bounds exceeded\n"; cerr << "Reason: " << err.z << '\n'; exit (8); } catch (...) { cerr << "Error: Unexpected exception occurred\n"; exit (8); } return (0); }
Monday, 30 April 2012
Program in C++ for Exception Handling
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment