|
CMPT 212
Fall 1997
|
|
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;
}
}
float square_root(float) throw (char *); void other_func() throw (int, char *); void f() throw (); // throws no execptions.
class myException {
public:
myException(int i) { ... }
void display() { ... }
...
}:
void f() {
try {
...
throw myException(3);
...
}
catch (myException& e) {
e.display();
}
...
}
catch (derivedException& e) { ... }
Return to lecture notes index |
|
| This page is maintained by simpson@cs.sfu.ca. | Last updated on 24 Nov 1997. |