exception handling. c++ 2 outline throwing and handling exceptions exceptions of different types ...
TRANSCRIPT
![Page 1: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/1.jpg)
Exception Handling
![Page 2: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/2.jpg)
C++ 2
Outline
Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing an exception
![Page 3: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/3.jpg)
C++ 3
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 throw statement.
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 the try and catch blocks.
![Page 4: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/4.jpg)
C++ 4
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.
![Page 5: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/5.jpg)
C++ 5
Example
The MyVector class with exception handling in the Sum member function.
![Page 6: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/6.jpg)
C++ 6
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();
};
![Page 7: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/7.jpg)
C++ 7
The Copy Constructor
MyVector::MyVector(const MyVector & v)
{dim = v.dim;elem = new int[dim];for(int i=0; i < dim; i++)
elem[i] = v.elem[i];}
![Page 8: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/8.jpg)
C++ 8
The Sum Member Function
MyVector MyVector::Sum(MyVector & v)
{if (dim != v.dim)
throw "Different size";int* x = new int[dim];
![Page 9: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/9.jpg)
C++ 9
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;
}
![Page 10: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/10.jpg)
C++ 10
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();
![Page 11: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/11.jpg)
C++ 11
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();
![Page 12: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/12.jpg)
C++ 12
The try and catch Blocks
try {cout << "sum of vectors: ";VektX.Sum(VektY).Display();cout << "No error\n";
}catch(const char *s) {
cout << "Error! " << s << endl;}
}
![Page 13: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/13.jpg)
C++ 13
Output (the same size)
dimension of X = 2the X vector : 1 3dimension of Y = 2the Y vector : 2 4sum of vectors: 3 7No error
![Page 14: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/14.jpg)
C++ 14
Output (different size)
dimension of X = 2the X vector : 1 3dimension of Y = 3the Y vector : 2 4 6sum of vectors: Error! Different size
![Page 15: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/15.jpg)
C++ 15
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!
![Page 16: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/16.jpg)
C++ 16
Example
#include <iostream>#include <stdlib.h>#include <time.h>using namespace std;
![Page 17: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/17.jpg)
C++ 17
The Print_Nonzero Function
template <class T>void Print_Nonzero(T x) {
if ( x == static_cast<T>(0) )throw static_cast<T>(0);
cout << typeid(x).name() << " " << x << endl;
}
![Page 18: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/18.jpg)
C++ 18
The main Function
int main() {
srand( (unsigned)time( NULL ) );// ...
![Page 19: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/19.jpg)
C++ 19
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;
![Page 20: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/20.jpg)
C++ 20
The try Block
case 2: Print_Nonzero( static_cast<double>( rand() % 2 ) ); break;
default: Print_Nonzero( rand() % 2 ); break;
}}
![Page 21: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/21.jpg)
C++ 21
Catch Blocks
catch( int ) {cout << "Error: zero (int)\n";
}catch( long ) {
cout << "Error: zero (long)\n";}catch( ... ) {
cout << "Error: zero (floating point)\n";}
}
![Page 22: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/22.jpg)
C++ 22
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 a
std::bad_alloc exception, but if we don’t want to occur this, then we have to use the form new(std::nothrow).
![Page 23: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/23.jpg)
C++ 23
Re-throwing an Exception
Use the throw;
statement in the catch block.
![Page 24: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/24.jpg)
C++ 24
Empty Class
#include <iostream>using namespace std;
class DivisionByZero{}; // use it only
// for handling exceptions
![Page 25: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/25.jpg)
C++ 25
The f Function
double f(double x) {if ( x == 0.0 )
throw DivisionByZero();return 1.0 / x;
}
![Page 26: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/26.jpg)
C++ 26
The g Function
double g(char *s, double x) {double y = 0.0;cout << s;try
{ y = f(x);
}
![Page 27: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/27.jpg)
C++ 27
The g Function
catch( DivisionByZero ) {
cout << "Error in g: ";//throw;
}return y;
}
![Page 28: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/28.jpg)
C++ 28
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";}
}
![Page 29: Exception Handling. C++ 2 Outline Throwing and handling exceptions Exceptions of different types The new operator and the exceptions Re-throwing](https://reader036.vdocument.in/reader036/viewer/2022062304/56649f275503460f94c3eaec/html5/thumbnails/29.jpg)
C++ 29
Output
The number = 0The inverse: Error in g: 0 But if we re-throw the exception
(uncomment throw in function g), then:
The number = 0The inverse: Error in g: Division by zero.