Bad Re-throw collection but crash at runtime
The following code will compile, but fail at runtime:
int main() {
try {
throw;
}
catch(...){
cout<<"In catch";
}
return 0;
}
Result: "Unhandled exception at 0x7c812a5b in hello.exe: Microsoft C ++ exception: [rethrow] @ 0x00000000"
Why does the compiler allow you to compile your code? it doesn't look so hard for the compiler to check if this code is part of the catch block or not.
source to share
There are probably millions of bugs that compilers can catch if compiler writers put enough work into them. But it is up to these compiler authors to judge if the work is worth it. In this case, they have not decided (and I agree with them). In the meantime, there is no need to worry about it.
Not so easy. You could call this function from within the catch block in some other function.
The concept of a so called exception handler is described in this answer .
source to share
You can place the throw in a function that is called from within the catch block. Sometimes useful if you have general handling for an exception class:
void handleXExceptions()
{
try {
throw;
} catch (XA&) {
...
} catch (XB&) {
...
} catch (X&) {
assert("Update handleXExceptions" == NULL);
}
}
void f() {
try {
...
} catch (X&) {
handleXExceptions();
}
}
void g() {
try {
...
} catch (X&) {
handleXExceptions();
}
}
source to share