#include <iostream>
using namespace std;
struct BaseException {
virtual void foo() {
cout << "BaseException::foo()\n";
}
int n;
};
struct DerivedException: BaseException {
DerivedException() {
}
virtual void foo() {
cout << "DerivedException::foo()\n";
}
private:
// error: ‘DerivedException::DerivedException(const DerivedException&)’ is private
// DerivedException(const DerivedException&) {
//}
};
void fn() {
throw DerivedException();
}
void f1() {
try {
fn();
} catch(BaseException& e) {
throw;
}
}
int main() {
try {
f1();
// throw int(100);
} catch(BaseException& e) {
e.foo();
} catch(int i) {
cout << i << endl;
}
}
你抛的东西可以随便抛,只是你抛的声明类型,你就catch什么类型,
catch(...) 表示捕获所有的类型