c++ try- except 语句
我看到这篇关于检测 VMWare 或 Virtual PC 的文章
http://www.codeproject.com/KB/system/VmDetect.aspx
我看到他们使用某种 try- except 语句。
所以我在 MSDN 中查找了它: http: //msdn.microsoft.com/en-us/library/s58ftw19%28v=vs.80%29.aspx
我不明白为什么要使用try- except 而不是传统的 try-catch。 它只是为我提供有关异常的附加信息吗?
如果是这样,当我使用随附文章中的代码时,我可以使用 try-catch,对吗?
谢谢 :)
I came across this article about detecting VMWare or Virtual PC
http://www.codeproject.com/KB/system/VmDetect.aspx
and I saw that they use some kind of try-except statement.
So I looked it up in the MSDN: http://msdn.microsoft.com/en-us/library/s58ftw19%28v=vs.80%29.aspx
and I don't understand why would I use a try-except instead of the good old try-catch.
does it just give me additional information about the exception?
If so, I can use a try-catch when I use the code from the attached article, right?
thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
__try
/__ except
是一个try
/catch
,用于不同类型的异常。您可以捕获硬件异常,例如浮点违规、错误的指针取消引用等,但不能捕获 C++ 异常。这称为结构化异常处理 (SEH),如果您知道去哪里查找,MSDN 上有很多相关内容。在这种情况下,他们用它来检测无效指令。这是他们尝试执行 x86 不支持的指令的地方,而虚拟机则使用它们。如果你在真实的CPU上运行,那么你会得到一个无效指令异常,如果你在虚拟机上运行,你只是与它交谈。
__try
/__except
is atry
/catch
, for a different kind of exception. You can catch hardware exceptions like floating point violation, bad pointer de-reference, etc, and not C++ exceptions. This is referred to as Structured Exception Handling, or SEH, and MSDN has quite a bit on it if you know where to look.In this case, they're using it to detect invalid instructions. This is where they attempt to execute instructions that x86 doesn't support, and virtual machines use them. If you're running on a real CPU, then you will get an invalid instruction exception, and if you're running on a virtual machine, you just talked to it.
MSDN 通常对所有这些都不清楚,但是
__try
/__except
处理的异常不是 C++ 异常,而是系统异常。像分段错误这样的事情。MSDN is typically unclear about all of this, but the exceptions dealt with by
__try
/__except
are not C++ exceptions, but system exceptions. Things like Segmentation Fault.__try
和__ except
是 结构化异常处理,这是一种与标准异常处理模型不同的异常处理模型,因为它以与软件异常相同的方式处理硬件异常,请参阅链接以获取信息。the
__try
and__except
are part of structured exception handling, this is a different exception handling model than the standard one, as it handles hardware exceptions identically to software ones, see the link for information.在实际的 C++ 标准开始包含异常之前,Microsoft 为 Microsoft C++ 创建了结构化异常处理。因此,在 Windows 上,存在的所有异常都是 SEH 异常,但并非所有异常都是 C++ 异常。
__try / __except 是一种捕获 SEH 异常的方法(意外地,也捕获 C++ 异常)。 try/catch 是仅捕获 C++ 异常的方法。我还记得不能在一个函数中同时使用这两种功能是有限制的,但解决这个问题很容易。
使用时,只需使用 try/catch 来处理任何异常。如果有人明确向您抛出 SEH 异常(除以零、空指针取消引用等),请捕获它并尽快将其转换为常规程序流,例如将其转换为常规异常或停止软件。
Microsoft created Structured Exception Handling for Microsoft C++ before the actual C++ standard started to include exceptions as well. On Windows, therefore, all exceptions that exist are SEH exceptions, but not all of those are C++ exceptions.
__try / __except is a way to catch SEH exceptions (and accidentally, also C++ exceptions). try/catch is the way to catch only C++ exceptions. I also recall that there's a limit to not being able to use both in one function, but it's easy to work around that.
For use, just use try/catch for any exceptions. If somebody explicitly throws you a SEH exception (divide by zero, null pointer dereference etc.), catch it and convert it to regular program flow asap, such as making it into a regular exception or halting the software.
__try
、__ except
和__finally
子句适用于 Structured Exception Handling,这是针对Windows抛出的异常的异常处理机制。它们与 C++ 异常不同。The
__try
,__except
and__finally
clauses are for Structured Exception Handling, this is an exception handling mechanism for exceptions thrown by Windows. They're not the same as C++ exceptions.