2

你好,再次对不起

我正在阅读英特尔的“Detecting Multiprocessor Topology in IA-32 Architecture”。我正在重新编写示例。但是我在代码中读到了这句话 __try 和 __except。

我在 MSDN Microsoft 网页上找到了一些信息,但我使用的是 gcc 编译器/链接器

__try 和 __except 是 gcc 中的有效句子吗?__try 和 __except 在 *nux 环境中是可移植的句子吗?确实存在比 __try 和 __except 语句更好的模式用于 C 中的异常处理?

提前致谢。

4

4 回答 4

3

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.

于 2012-09-20T13:24:08.503 回答
1

__try 和 __except 是(如 Microsoft 所述)C/C++ 语言的扩展。所以它在 gcc 中不可移植。我认为微软把这些放进去是为了让纯 C(不使用 C++ 编译器)可以有类似于异常处理的东西。* S *tructured * E *xception * H *andling (SEH) 是相当低级的,据我所知,甚至微软自己的 C++ 异常处理在引擎盖下也使用 SEH。

我对整个事情的看法是,如果您看到 __try 和 __except,只需将其替换为 try 和 catch,然后使用 C++ 编译器进行编译。您当然必须更换传递给 __except 的过滤器,但这没什么大不了的。

于 2012-09-20T13:26:26.273 回答
1

C 标准没有异常处理支持。C++ 可以。有许多使用 setjmp 的 try/catch 实现。

于 2012-09-20T13:08:39.033 回答
0

您可能想查看以下链接:

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

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

于 2012-09-20T13:13:14.473 回答