throw new std :: exception vs throw std :: exception [英] throw new std::exception vs throw std::exception

查看:743
本文介绍了throw new std :: exception vs throw std :: exception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看着一些代码,我偶然发现:

while looking at some code I stumbled onto:

throw /*-->*/new std::exception ("//...

我一直认为你不需要/ t在这里使用 new

正确的方式是正确的,如果有,是否有什么区别?

and I always thought that you don't need/you shouldn't use new here.
What is the correct way, are both OK, if so is there any difference?

BTW从我可以看到,而grepping与PowerShell boost libs从不使用抛出新

BTW from what I can see while "grepping" with PowerShell boost libs never use throw new.

PS我也发现了一些使用 throw gcnew 的CLI代码,可以吗?

P.S. also I found some CLI code that uses throw gcnew. Is that OK?

推荐答案

抛出和捕获异常的常规方法是抛出异常对象并通过引用捕获它(通常 const 引用)。C ++语言需要编译器生成适当的代码来构造异常对象并在适当的时间正确清理它。

The conventional way to throw and catch exceptions is to throw an exception object and to catch it by reference (usually const reference). The C++ language requires the compiler to generate the appropriate code to construct the exception object and to properly clean it up at the appropriate time.

抛出指向动态分配对象的指针永远不是好主意,异常应该让你能够在面对错误条件时编写更健壮的代码。如果你以常规的方式抛出一个异常对象,你可以通过一个 catch(...)来确定它是否被catch子句捕获了正确的类型,无论是否被重新抛出,它将在适当的时间被正确销毁。 (唯一的例外是,如果它永远不会被捕获,但这是一个不可恢复的情况,无论哪种方式你看它。)

Throwing a pointer to a dynamically allocated object is never a good idea. Exceptions are supposed to enable you to write more robust code in the face of error conditions. If you throw an exception object in the conventional manner you can be sure that whether it is caught by a catch clause naming the correct type, by a catch (...), whether it is then re-thrown or not it will be destroyed correctly at the appropriate time. (The only exception being if it is never caught at all but this is a non-recoverable situation whichever way you look at it.)

如果你抛出一个指针动态分配的对象你必须确保无论调用堆栈看起来像你想抛出你的异常的点有一个catch块命名正确的指针类型,并有适当的 delete call。你的异常永远不会被 catch(...)捕获,除非该块重新抛出异常,然后被捕获为另一个捕获块,正确处理异常。

If you throw a pointer to a dynamically allocated object you have to be sure that whatever the call stack looks like at the point you want to throw your exception there is a catch block that names the correct pointer type and has the appropriate delete call. Your exception must never be caught by catch (...) unless that block re-throws the exception which is then caught be another catch block that does deal correctly with the exception.

实际上,这意味着您已经采取了异常处理功能,这将使编写强大的代码变得更容易,并且很难编写在任何情况下都正确的代码。这是留下的问题,几乎不可能作为库代码的客户端代码,不会期望这个功能。

Effectively, this means you've taken the exception handling feature that should make it easier to write robust code and made it very hard to write code that is correct in all situations. This is leaving aside the issue that it will be almost impossible to act as library code for client code that won't be expecting this feature.

这篇关于throw new std :: exception vs throw std :: exception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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