290 likes | 408 Views
Exception Handling. throw MyObj;. Outline. Throwing and handling exceptions Exceptions of different types The new ope rator and the exceptions Re-throwing an exception. Throwing and Handling Exceptions.
E N D
Exception Handling throw MyObj;
Outline • Throwing and handling exceptions • Exceptions of different types • The new operator and the exceptions • Re-throwing an exception C++
Throwing and Handling Exceptions • We throw an exception when we want to postpone the effect of an event (which is probably an error). • In order to accomplish this we use the throwstatement. • After the throw keyword we have to give an expression (a constant or a variable or maybe an object). • Exception handling can be done using thetryand catch blocks. C++
The try and catch Blocks • The try - catch construction: try compound-statement handler-sequence • The handler-sequence is composed of one or more handlers. Each handler has the form: catch(exception-declaration) compound-statement • The exception-declaration is similar to the formal parameters of functions. C++
Example • The MyVector class with exception handling in the Sum member function. C++
The Class Declaration class MyVector { private: int *elem; int dim; public: MyVector(int *e, int d); MyVector(const MyVector & v); ~MyVector(); void SquareVect(); MyVector Sum(MyVector & v); void Display(); }; C++
The Copy Constructor MyVector::MyVector(const MyVector & v) { dim = v.dim; elem = newint[dim]; for(int i=0; i < dim; i++) elem[i] = v.elem[i]; } C++
The Sum Member Function MyVector MyVector::Sum(MyVector & v) { if (dim != v.dim) throw"Different size"; int* x = newint[dim]; C++
The Sum Member Function for(int i = 0; i < dim; i++) x[i] = elem[i] + v.elem[i]; MyVector t(x, dim); delete [] x; return t; } C++
The main Function (VectX) int main() { int x[20]; int dimX; cout << "dimension of X = "; cin >> dimX; for(int i = 0; i < dimX; i++) x[i] = 2 * i + 1; MyVector VektX(x, dimX); cout << "the X vector : "; VektX.Display(); C++
VectY int y[20]; int dimY; cout << "dimension of Y = "; cin >> dimY; for(int i = 0; i < dimY; i++) y[i] = 2 * i + 2; MyVector VektY(y, dimY); cout << "the Y vector : "; VektY.Display(); C++
The try and catch Blocks try { cout << "sum of vectors: "; VektX.Sum(VektY).Display(); cout << "No error\n"; } catch(constchar *s) { cout << "Error! " << s << endl; } } C++
Output (the same size) dimension of X = 2 the X vector : 1 3 dimension of Y = 2 the Y vector : 2 4 sum of vectors: 3 7 No error C++
Output (different size) dimension of X = 2 the X vector : 1 3 dimension of Y = 3 the Y vector : 2 4 6 sum of vectors: Error! Different size C++
Exceptions of Different Types • We can have multiple catch blocks after a try block. • To catch everything use: catch ( ... ) compound-statement • The ... is part of the syntax. • The order of the catch blocks is important! C++
Example #include<iostream> #include<stdlib.h> #include<time.h> usingnamespace std; C++
The Print_Nonzero Function template <class T> void Print_Nonzero(T x) { if ( x == static_cast<T>(0) ) throwstatic_cast<T>(0); cout << typeid(x).name() << " " << x << endl; } C++
The main Function int main() { srand( (unsigned)time( NULL ) ); // ... C++
The try Block try { switch ( rand() % 5 ) { case 0: Print_Nonzero( static_cast<long>( rand() % 2 ) ); break; case 1: Print_Nonzero( static_cast<float>( rand() % 2 ) ); break; C++
The try Block case 2: Print_Nonzero( static_cast<double>( rand() % 2 ) ); break; default: Print_Nonzero( rand() % 2 ); break; } } C++
Catch Blocks catch( int ) { cout << "Error: zero (int)\n"; } catch( long ) { cout << "Error: zero (long)\n"; } catch( ... ) { cout << "Error: zero (floating point)\n"; } } C++
The new Operator and the Exceptions • If there isn’t enough memory then • in older versions of C++ the new operator simply returned zero • at present(C++ standard) throws astd::bad_allocexception, but if we don’t want to occur this, then we have to use the formnew(std::nothrow). C++
Re-throwing an Exception • Use the throw; statement in the catch block. C++
Empty Class #include<iostream> usingnamespace std; class DivisionByZero{}; // use it only // for handling exceptions C++
The f Function double f(double x) { if ( x == 0.0 ) throw DivisionByZero(); return 1.0 / x; } C++
The g Function double g(char *s, double x) { double y = 0.0; cout << s; try { y = f(x); } C++
The g Function catch( DivisionByZero ) { cout << "Error in g: "; //throw; } return y; } C++
The main function int main() { double number; cout << "The number = "; cin >> number; try { cout << g("The inverse: ", number) << endl; } catch( DivisionByZero ) { cout << "Division by zero.\n"; } } C++
Output The number = 0 The inverse: Error in g: 0 • But if we re-throw the exception (uncomment throw in function g), then: The number = 0 The inverse: Error in g: Division by zero. C++