这个
#include <stdexcept>
struct A /*: public std::exception*/ {
const char* what() const noexcept { return "this is A"; }
};
int main(){
throw A{};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
给我(上stderr):
terminate called after throwing an instance of 'A'
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)
如果我取消注释,死亡消息将变为:
terminate called after throwing an instance of 'A'
what(): this is A
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)
怎么std::terminate知道std::exception特别对待s?
我怎么能在自己的模仿中做到这一点set_terminate?我试过了
//...
int main(){
std::set_terminate([](){
printf("exception thrown\n");
std::exception_ptr eptr = std::current_exception();
std::exception* ptr = dynamic_cast<std::exception*>(eptr);
if (ptr)
puts(ptr->what());
});
throw A{};
}
Run Code Online (Sandbox Code Playgroud)
但它不会因为该dynamic_cast行而编译.
| 归档时间: |
|
| 查看次数: |
274 次 |
| 最近记录: |