Structured Exception Handling - Syntax confusion

Hello All,

I am trying to write an exception handling code for a kernel driver, but I am a bit confused regarding the versatility of the SEH in different documentation. For example, see here https://www.osronline.com/article.cfm?article=58
and here
https://msdn.microsoft.com/en-us/library/windows/hardware/ff546823(v=vs.85).aspx

My compiler (VS12) successfully compiles __try{} __except(){}, and _try{} _except(){}, but not try{} except(){}. However, I have seen people are using the latter syntax, and that is how it is documented in MSDN (see above link).

I was wondering if there is any difference between these structures, and id so which one is recommended for the kernel land.

Thanks.

xxxxx@datagardens.com wrote:

I am trying to write an exception handling code for a kernel driver, but I am a bit confused regarding the versatility of the SEH in different documentation. For example, see here https://www.osronline.com/article.cfm?article=58
and here
https://msdn.microsoft.com/en-us/library/windows/hardware/ff546823(v=vs.85).aspx

My compiler (VS12) successfully compiles __try{} __except(){}, and _try{} _except(){}, but not try{} except(){}. However, I have seen people are using the latter syntax, and that is how it is documented in MSDN (see above link).

I was wondering if there is any difference between these structures, and id so which one is recommended for the kernel land.

In part, it depends on whether you are writing code in C or C++.
Windows Structured Exception Handling is built-in to the compiler, and
is spelled with two underlines: __try and __except. If you are
writing C code, there is a chunk in <warning.h> that defines “try” and
“except” to be aliases for those two:

#ifndef cplusplus
#undef try
#undef except
#undef finally
#undef leave
#define try
try
#define except except
#define finally
finally
#define leave leave
#endif

But if you are writing C++ code, then “try” has a pre-defined meaning in
the language that can’t be altered, and that meaning cannot be used in
kernel code. Thus, you must use
try.

I believe _try is always simply an alias for __try.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.</warning.h>

> My compiler (VS12) successfully compiles __try{} __except()

Yes, use this for C.

This will be the same as Win32 SEH in user mode.

For C++, you have try/catch, which are OO (exception is a class, destructors are auto-called on unwind), are defined by C++ language designers (not by MS) and not limited to Windows, and are internally implemented on Windows using a special kind of Win32/NT SEH.

Note that NT kernel lacks these tiny assembler routines which are included to user-mode MSVCRT and are required to implement C++ EH. So, for NT kernel, you will need to implement them yourself, based on the calls of RtlUnwind etc.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

>try, catch, finally are C++ keywords and are used with C++ exceptions.

C++ has “finally”? new for me.

SEH is an entirely separate (not in implementation, but logically) construct.

Some details:

  • C++ catch(…) will catch all SEH/Win32 exceptions (note that you have no exception object in such a handler). Other forms of C++ catch() will not catch any Win32 exceptions.
  • Win32 SEH will catch C++ exceptions as SEH exceptions with special exception code of “MS C++ exception”.
  • if you call _set_se_translator() (MS-proprietary), then your translator routine is called on each SEH exception very early, and can do C++ “throw”, thus converting SEH exception to C++ one.

You will often see these refereed to a synchronous and asynchronous exceptions

These terms are just plain misleading in any context.

The goal under these terms is to say “exception can be generated by CPU itself without explicit desire of your code” or “exception can be generated only by explicit “throw” operator or call in your code”.

Originally, C++ exceptions are second class, but if you call _set_se_translator(), then Win32 SEH are mapped to C++ exceptions.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com