__try and __exception portability
Asked Answered
S

4

3

Hello and excuse me again

I am reading "Detecting Multiprocessor Topology in IA-32 Architecture" from Intel. I was recoding the example. However I read this sentences __try and __except in the code.

I found MSDN Microsoft web page some information but I am using gcc compiler/linker

are __try and __except valid sentences in gcc? are __try and __except portable sentences on *nux environments? do exist a better mode than __try and __except sentences for exception handling in C?

Thanks in advance.

Schutzstaffel answered 20/9, 2012 at 13:2 Comment(0)
S
4

General considerations

According to MSDN, __try and __except are extensions to the C language. As such, they are not portable out of the box.

For some applications it might be possible to mimic their behaviour using macros and a setjmp/longjmp constructs. In general this will not be the case. In particular, MSDN states that

The __except expression executes in the scope of the __try body.

which cannot be achieved using macros alone.

Your questions

are __try and __except valid sentences in gcc?

No, they are not, as gcc does not support this Microsoft dialect of C.

are __try and __except portable sentences on *nux environments?

No, they are not, although there are ways to achieve similar exception handling on *nix as well.

do exist a better mode than __try and __except sentences for exception handling in C?

There is a strong reason to use C++ when you do exception handling. C++ scoped objects with destructors are much more robust in the presence of exceptions than standard C resource management. So while there are implementations which provide tryand catch for C one way or another, I'd advise against them and favor C++ instead. Exception handling in C++ is part of the standard and therefore supported by all standards-compliant compilers, without portability issues.

Sox answered 20/9, 2012 at 13:24 Comment(1)
Not exact. C++ try/catch are capable to handle only c++ exceptions. The whom that originated by throw somewhere in the code. While __try/__except is SEH handler that handle any kind of exception, included null dereference, divide by zero and more. Depending on the scenario and the desired level of robustness, on Windows there may be good reason to prefer the SEH handler over c++Shaquitashara
P
1

C standard has no exception handling support. C++ does. There are a number of try/catch implementations using setjmp.

Patina answered 20/9, 2012 at 13:8 Comment(0)
I
1

The __try and __except is (as Microsoft states) an extension to the C/C++ language. So it is not portable in gcc. I think Microsoft put these in so that pure C (not using a C++ compiler) could have something similar to exception handling. *S*tructured *E*xception *H*andling (SEH) is pretty low level, and as far as I can remember even Microsoft's own C++ exception handling uses SEH under the hood.

My take on the whole thing is, if you are see __try and __except, just replace it with try and catch, and just compile with a C++ compiler. You will of course have to replace the filter that gets passed to __except, but that's not a big deal.

Introduce answered 20/9, 2012 at 13:26 Comment(0)
P
0

You might want to take a look at the following links:

gcc.gnu.org/onlinedocs/libstdc++/manual/using_exceptions.html

www.autohotkey.com/community/viewtopic.php?t=31475

Paradigm answered 20/9, 2012 at 13:13 Comment(1)
C++ exceptions are not the same kind of exceptions caught in Microsoft's C with __try/__catch/etc. The former are language exceptions, the latter are CPU exceptions (see SEH, Structured Exception Handling on MSDN).Juster

© 2022 - 2024 — McMap. All rights reserved.