我在linux上使用gcc来编译C++代码.有些例外情况不应该处理,应该关闭程序.但是,我希望能够显示异常字符串:
例如:
throw std::runtime_error(" message");不显示消息,仅显示错误类型.我也想显示消息.有办法吗?
它是一个库,我真的不想把catch语句和库用户决定.但是,现在库用户是fortran,它不允许处理异常.原则上,我可以将处理程序放在包装器代码中,而不是如果有方法的话
小智 21
标准异常有一个虚what()方法,可以为您提供与异常相关的消息:
int main() {
try {
// your stuff
}
catch( const std::exception & ex ) {
cerr << ex.what() << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
你可以用main写:
try{
}catch(const std::exception &e){
std::cerr << e.what() << std::endl;
throw;
}
Run Code Online (Sandbox Code Playgroud)