Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ try-except statement

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 :)

like image 868
Idov Avatar asked Dec 16 '22 12:12

Idov


2 Answers

__try/__except is a try/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.

like image 166
Puppy Avatar answered Dec 19 '22 02:12

Puppy


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.

like image 44
Lightness Races in Orbit Avatar answered Dec 19 '22 02:12

Lightness Races in Orbit