Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bad Re-throw compiles but crash at runtime

Tags:

c++

exception

Following code will compile but crash at run time:

int main() {

    try {
        throw;
    }
    catch(...){
        cout<<"In catch";
    }
     return 0;

}

Result: “Unhandled exception at 0x7c812a5b in hello.exe: Microsoft C++ exception: [rethrow] @ 0x00000000”

Why compiler allows the code to compile? it looks not so difficult job for compiler to check if this code is part of catch block or not.

like image 401
Learner Avatar asked Nov 26 '25 23:11

Learner


1 Answers

From C++ Standard (15.1.8)

If no exception is presently being handled, executing a throw-expression with no operand calls std::terminate()

As standard allows it and gives clear semantics, a compiler can only conform to it.

like image 171
Tadeusz Kopec for Ukraine Avatar answered Nov 28 '25 15:11

Tadeusz Kopec for Ukraine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!