Nicolas Alvarez wrote:
> template<typename E>
> void my_throw_exception(E& exc) {
> //do the backtrace() dance and store the results somewhere in exc
> throw exc;
> }
And yes, you do need the template. The 'throw' statement uses the *static*
type of the variable. So for example:
class MyError: public std::exception {};
void my_throw_exception(std::exception& exc) {
throw exc;
}
void foo(int test) {
switch (test) {
case 1:
throw std::exception();
break;
case 2:
throw MyError();
break;
case 3:
my_throw_exception(std::exception());
break;
case 4:
my_throw_exception(MyError()); // <- MyError gets sliced here
break;
}
}
int main() {
try {
foo(1);
} catch(MyError& e) {
std::cerr << "MyError thrown\n";
} catch(std::exception& e) {
std::cerr << "std::exception thrown\n";
}
}
It will only print "MyError thrown" if you pass 2 to foo().
Post a reply to this message
|