|
CMPT 212
Spring 1998
|
|
Read section Exceptions of chapter 14 from C++ Primer Plus (2nd ed.), by Stephen Prata. |
int i;
...
try {
if (i < 0) {
throw "negative number";
}
else {
cout << i << endl;
}
}
catch (char * str) {
cout << str << endl;
}
float square_root(float f) {
if (f < 0) {
throw "negative number";
}
else {
return sqrt(f);
}
}
void func() {
float f;
cin >> f;
try {
cout << square_root(f) << endl;
}
catch (char * str) {
cout << str << endl;
}
catch (int i) {
cout << i << endl;
}
catch (...) {
cout << "unknown exception" << endl;
}
}
Return to lecture notes index |
|
| This page is maintained by simpson@cs.sfu.ca. | Last updated on 27 Mar 1998. |