为什么catch没有处理这个错误C++

Ram*_*Ram -3 c++ exception

当我在linux机器上运行它时,我希望catch块能够捕获异常.而我得到了分段错误.为什么是这样 ?它不应该打印"抓住异常"

void handle_eptr(std::exception_ptr eptr) // passing by value is ok
{
    try {
        if (eptr) {
            std::rethrow_exception(eptr);
        }
    } catch(const std::exception& e) {
        std::cout << "Caught exception \"" << e.what() << "\"\n";
    }
}

int main()
{

    char *c =(char*) 0x10;



    std::exception_ptr eptr;
    try {
        std::string s = c;

    } catch(...) {
        std::cout<< "Caught exception";
        //eptr = std::current_exception(); // capture
    }
   // handle_eptr(eptr);
    std::cout << "Normal Exit";
}
Run Code Online (Sandbox Code Playgroud)

R S*_*ahu 5

特定

char *c =(char*) 0x10;
Run Code Online (Sandbox Code Playgroud)

以下行导致未定义的行为.

std::string s = c;
Run Code Online (Sandbox Code Playgroud)

之后程序的行为可以是任何东西.在此之后尝试理解程序的行为是没有意义的.

有关未定义行为的更多信息可以在Undefined,unspecified和实现定义的行为中找到.

  • @Ram没有被捕的例外.*未定义的行为*不会引发异常 (3认同)