Rev | Line | |
---|
[5442] | 1 | // This example illustrates constructors and
|
---|
| 2 | // destructors in exception handling.
|
---|
| 3 |
|
---|
| 4 | #include <string.h> // needed for strcpy
|
---|
| 5 | #include <iostream.h>
|
---|
| 6 | class Matherr { public: char errname[30]; };
|
---|
| 7 | class DivideByZero : public Matherr
|
---|
| 8 | {
|
---|
| 9 | public:
|
---|
| 10 | DivideByZero() {strcpy (errname, "Division by zero");}
|
---|
| 11 | };
|
---|
| 12 | double divide(double a, double b)
|
---|
| 13 | {
|
---|
| 14 | if (b == 0) throw DivideByZero();
|
---|
| 15 | return a/b;
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | void main()
|
---|
| 19 | {
|
---|
| 20 | double a=7,b=0;
|
---|
| 21 | try {divide (a,b);}
|
---|
| 22 | catch (Matherr xx)
|
---|
| 23 | {
|
---|
| 24 | cout << xx.errname << endl;
|
---|
| 25 | }
|
---|
| 26 | }
|
---|
| 27 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.