C++ __try/__except 块或 try / catch 块哪个更好用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3730654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's better to use, a __try/__except block or a try / catch block?
提问by Zain Rizvi
I'm wondering which is the better way to catch exceptions that I throw: is it a __try / __except block or a try / catch block?
我想知道哪个是捕获我抛出的异常的更好方法:它是 __try / __except 块还是 try / catch 块?
I'm writing in C++ and the program will only be used on Windows, so portability is not an issue.
我正在用 C++ 编写,该程序只能在 Windows 上使用,因此可移植性不是问题。
Thanks!
谢谢!
采纳答案by Billy ONeal
You should use a try
/catch
block.
您应该使用try
/catch
块。
As others have already answered, __try
/ __except
is for catching SEH (windows generated errors) not for catching general exceptions.
正如其他人已经回答的那样,__try
/__except
用于捕获 SEH(Windows 生成的错误)而不是用于捕获一般异常。
Most importantly, __try
and __catch
may not run C++ destructors or correctly unwind the stack when an exception is thrown.
最重要的是,__try
和__catch
可能不运行的C ++析构函数或正确地展开堆栈时则抛出异常。
Except in rare cases, you should never try to catch SEH exceptions.
除了极少数情况外,您永远不应该尝试捕获 SEH 异常。
EDIT:Well, I was positive on this (it's what I've always been told), but @Hans says that apparently there is a compiler switch you can use to change this. I think the docs on /EHa
are misleading, or at least incomplete, on what happens here. If someone finds definitive docs which prove this wrong, I'll happily remove this answer.
编辑:嗯,我对此持肯定态度(这是我一直被告知的),但@Hans 说显然有一个编译器开关可以用来改变它。我认为/EHa
关于这里发生的事情的文档具有误导性,或者至少是不完整的。如果有人找到证明此错误的权威文档,我会很乐意删除此答案。
Even if it turns out this is false, you should still use try
and catch
simply because they are standard, while __try
and __except
are not.
即使事实证明这是假的,你还是应该使用try
和catch
仅仅因为他们是标准配置,__try
而__except
不是。
回答by Hans Passant
They are two verydifferent things. try/catch are the familiar C++ keywords you know. __try/__except
is used to catch SEH exceptions. Exceptions raised by Windows itself, like DivisionByZero or AccessViolation. It is well described in the MSDN Library articlefor it.
它们是两个非常不同的东西。try/catch 是您熟悉的 C++ 关键字。 __try/__except
用于捕获 SEH 异常。Windows 本身引发的异常,例如 DivisionByZero 或 AccessViolation。MSDN 库文章中对此进行了很好的描述。
You can also use it to catch C++ exception because it leverages the Windows SEH feature. You however can't get the thrown exception object out of it so there will be zero context if you actually want the handle the exception. Which is madness. The number one approach is to not ever catch SEH exceptions, they are always gross. If you do need to marry the two then use _set_se_translator() to convert the SEH exception to a C++ exception.
您还可以使用它来捕获 C++ 异常,因为它利用了 Windows SEH 功能。但是,您无法从中获取抛出的异常对象,因此如果您确实想要处理异常,则上下文为零。这是疯狂。第一种方法是永远不要捕获 SEH 异常,它们总是很糟糕。如果您确实需要将两者结合,则使用 _set_se_translator() 将 SEH 异常转换为 C++ 异常。
回答by Randolpho
__try/__except
is designed for calling Win32 C code which doesn't support exceptions but does use a structured error code / handling mechanism. __try/__except
will translate C errors into an exception block similarto C++ try/catch.
__try/__except
专为调用不支持异常但使用结构化错误代码/处理机制的 Win32 C 代码而设计。__try/__except
将 C 错误转换为类似于C++ try/catch的异常块。
For more information, see this MSDN article.
有关详细信息,请参阅此 MSDN 文章。
回答by C?t?lin Piti?
Standard C++ uses try/catch blocks, so I would recommend using them, if you need the "standard" exception mechanism, based on standard C++ library.
标准 C++ 使用 try/catch 块,因此如果您需要基于标准 C++ 库的“标准”异常机制,我建议使用它们。
However, if you plan to use Structured Exception Handling provided through Windows SDK (see here), then use __try
/__except
.
但是,如果您打算使用通过 Windows SDK 提供的结构化异常处理(请参阅此处),请使用__try
/ __except
。
回答by Rob Kennedy
Once you've thrown something, you no longer have much choice about how to catch it. If you throw C++ exceptions (i.e., with throw
), then use try
/catch
. If you throw Windows exceptions (i.e., with RaiseException
), then use __try
/__except
. Trying to mix them will just be adding unnecessary hassle to your life.
一旦你扔了东西,你就不再有太多选择如何抓住它了。如果您抛出 C++ 异常(即使用throw
),则使用try
/ catch
。如果您抛出 Windows 异常(即使用RaiseException
),则使用__try
/ __except
。尝试混合它们只会给您的生活增添不必要的麻烦。