C ++运行时,显示异常消息 [英] C++ runtime, display exception message
问题描述
我在Linux上使用gcc来编译C ++代码。
有些异常不应该处理,应该关闭程序。
但是,我希望能够显示异常字符串:
I am using gcc on linux to compile C++ code. There are some exceptions which should not be handled and should close program. However, I would like to be able to display exception string:
例如:
抛出std :: runtime_error( message);
不显示消息,仅显示错误类型。
我也想显示消息。
有办法吗?
throw std::runtime_error(" message");
does not display message, only type of error.
I would like to display messages as well.
Is there way to do it?
这是一个库,我真的不想放入catch语句让库用户决定。
但是,现在的库用户是fortran,它不允许处理异常。
原则上,我可以将处理程序放入包装器代码中,但是如果没有解决办法,则不要这样做
it is a library, I really do not want to put catch statements and let library user decide. However, right now library user is fortran, which does not allow to handle exceptions. in principle, I can put handlers in wrapper code, but rather not to if there is a way around
推荐答案
标准异常具有虚拟的 what()
方法,该方法为您提供与异常相关的消息:
Standard exceptions have a virtual what()
method that gives you the message associated with the exception:
int main() {
try {
// your stuff
}
catch( const std::exception & ex ) {
cerr << ex.what() << endl;
}
}
这篇关于C ++运行时,显示异常消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!