c++ - 抛出 std::exception( 或派生类型) 的其他东西有什么好处吗

标签 c++ exception-handling

是否有任何优势或用例来抛出 std::exception( 或派生类型) 的其他东西。

例如throw 1;throw "error";

换句话说为什么 c++ 标准允许它。

最佳答案

根据 §15.1 [除外]:

Exception handling provides a way of transferring control and information from a point in the execution of a thread to an exception handler associated with a point previously passed by the execution.

信息这个词说明了一切,它可以是任何事物,例如对象、数字……。

标准中没有任何内容规定您必须抛出 std::exception。换句话说,也许有人想抛出他自己的异常对象。

也许有人想使用异常处理来处理一些与正常异常相去甚远的事情。

关于c++ - 抛出 std::exception( 或派生类型) 的其他东西有什么好处吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16794972/

相关文章:

c# - Web API 2 OData错误消息作为XML返回

c++ - 为什么 move on const 对象有效?

c++ - X DevAPI mysqlx::Session() over linux socket 失败并显示 “CDK Error: unexpected message”

jakarta-ee - JPA数据访问对象——异常处理和回滚

python - 更改 python 中未捕获异常处理的默认行为

c++ - 在包含 std::string 的函数上使用 throw() 后缀是否安全?

c# - 在单独的线程上捕获未处理的异常

c++ - 视觉 C++ 2008;将库添加到编译过程?

c++ - googletest:如何从内部方法跳过测试

c++ - 输入的整数验证