Line | |
---|
1 | // This example illustrates the basic use of
|
---|
2 | // try, catch, and throw.
|
---|
3 |
|
---|
4 | #include <iostream.h>
|
---|
5 | #include <stdlib.h>
|
---|
6 | class IsZero { /* ... */ };
|
---|
7 | void ZeroCheck( int i )
|
---|
8 | {
|
---|
9 | if (i==0)
|
---|
10 | throw IsZero();
|
---|
11 | }
|
---|
12 | void main()
|
---|
13 | {
|
---|
14 | double a;
|
---|
15 |
|
---|
16 | cout << "Enter a number: ";
|
---|
17 | cin >> a;
|
---|
18 | try
|
---|
19 | {
|
---|
20 | ZeroCheck( a );
|
---|
21 | cout << "Reciprocal is " << 1.0/a << endl;
|
---|
22 | }
|
---|
23 | catch ( IsZero )
|
---|
24 | {
|
---|
25 | cout << "Zero input is not valid" << endl;
|
---|
26 | }
|
---|
27 | cout << "Finished." << endl;
|
---|
28 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.