c ++异常,what()可以为NULL吗? [英] c++ exceptions, can what() be NULL?

查看:86
本文介绍了c ++异常,what()可以为NULL吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

捕获的std :: exception是否可以将what()设为NULL?

Can a caught std::exception ever have what() being NULL?

是否正在检查开销以下的e.what()?

Is the checking for e.what() below overhead?

//...
}
catch (const std::exception& e)
{
  std::string error;
  if(e.what())
    error = e.what();
}


推荐答案

字符串的内容是

编辑:将其隐藏起来。标准说:

Belay that. The standard says:

virtual const char* what() const throw();
5 Returns: An implementation-defined NTBS.

因此它必须返回一个字符串,而不仅仅是一个指针。字符串不能为 NULL 。正如其他人指出的那样,很容易导出其 what()确实返回 NULL 的异常,但是我不是确保这些东西如何符合标准。当然,如果您要在自己的异常类中实现what(),我认为允许它返回NULL是非常糟糕的做法。

So it must return a string, not just a pointer. And a string cannot be NULL. As others have pointed out it is easy to derive exceptions whose what() does return NULL, but I'm not sure how such things fit into standards conformance. Certainly, if you are implementing what() in your own exception class, I would consider it very bad practice to allow it to return NULL.

更多:

有关进一步的问题,以解决 what()是否可以返回NULL以及类似的令人兴奋的问题,请参见通过继承扩展C ++标准库?

For a further question addressing whether what() can return NULL, and similar exciting issues, please see Extending the C++ Standard Library by inheritance?

这篇关于c ++异常,what()可以为NULL吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆