當我在linux機器上運行這個時,我期望catch塊捕獲異常。而我得到了分段錯誤。爲什麼是這樣 ?如果它不打印「被捕獲的異常」爲什麼catch沒有處理這個錯誤C++
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";
}
[什麼例外](http://en.cppreference.com/w/cpp/string/basic_string/basic_string)? – LogicStuff
如果不打印「捕獲的異常」 – Ram
您的期望是非法的地址解引用將導致C++異常。事實並非如此。你已經觸發了UB,UB可以做任何事情;包括拋出一個異常,打開一年的聖誕樹上的燈光,重新格式化硬盤,... – pm100