std :: exception :: what()的目的是什么? [英] What is the purpose of std::exception::what()?
问题描述
我只能想到以下使用std :: exception :: what()的情况:
I can only think of the following situations where std::exception::what() is used:
- 用于调试.在我的Visual Studio中,要查看e.what(),我必须将其手动添加到监视列表.拥有成员std :: string更好吗(调试器直接在对象检查器中显示它),并且仅将其包括在非NDEBUG构建中?至少他们应该在NDEBUG版本中禁用what().
-
输出它,例如
MessageBox(e.what())
或cout<<e.what()
.据我所知,这些消息对许多用户都没有用.例如,当我尝试重命名一个不存在的文件时:
- For debug purpose. In my Visual Studio to see e.what() I have to manually add it to the watch list. Isn't it better to have a member std::string (so the debugger directly shows it in the object inspector), and only include it in non-NDEBUG builds? At least they should disable what() in NDEBUG builds.
Output it, e.g.
MessageBox(e.what())
orcout << e.what()
. As far as I know these messages are useless for many users. For example when I try to rename a file that doesn't exist:
boost :: filesystem :: rename:系统查找指定的文件.:"D:\ MyDesktop \ 4","D:\ MyDesktop \ 5"
(中文意思是系统找不到指定的文件.")用户如何解密混合的东西?另外,它是一个 const char *
,而不是像 const platform_char *
这样的东西,在Windows中可能会出现unicode问题.
(The Chinese words means "The system cannot find the file specified.") How can the users decrypt the mixed things? Also, it is a const char*
instead of something like const platform_char*
, which may have unicode problems in Windows.
那么我应该在哪里使用std :: exception :: what()?没用吗?
So where should I use std::exception::what()? Is it useless?
推荐答案
程序员应该从 std :: exception
和taylor what()
派生一个类.具体要求.然后它会非常有用.
A programmer is supposed to derive a class from std::exception
and taylor what()
to the specific requirements. Then it can be very useful.
向后报告某物(例如以纯文本格式记录)也很有用,这就是该标准要求具体的 std :: exception :: what()
的原因而不是纯虚函数.
It's also useful to report something back (e.g. in plain text for logging), which is why the standard mandates a concrete std::exception::what()
rather than a pure virtual function.
这篇关于std :: exception :: what()的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!