可移植地处理C ++中的特殊错误 [英] Portably handle exceptional errors in C++

查看:141
本文介绍了可移植地处理C ++中的特殊错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个Visual C ++应用程序移植到GCC(应该建立在MingW和Linux上)。
$ b

现有代码使用 __ try {...} __except(1){...} 会在几个地方阻塞,因此几乎没有任何内容(可能会导致内存不足错误?日志记录。

使用GCC做类似的选项有哪些?



编辑:感谢指向/ EH选项在Visual Studio中,我现在需要的是一些关于如何在Linux上处理信号的示例。我从2002年已找到此消息



除了 SIGFPE SIGSEVG 之外,还有什么其他信号可以观察为了? (主要关心可能从 引发的行为做错的行为)

赏金信息
我希望我的应用程序能够在退出之前尽可能多地记录错误条件。


我可以得到什么样的信号,而且之后通常不可能记录错误信息? (内存不足,还有什么?)



如何以便携的方式处理异常和(最重要的)信号,以至于代码在Linux上至少可以工作并且MingW平台。 #ifdef是确定的。

我不只是有一个包装进程记录失败的原因是出于性能方面的原因,我保存了一些数据写入磁盘直到最后分钟,所以如果出现问题,我想尽可能在​​退出前写出数据。

解决方案

try {xxx } catch(...){xxx}将更具可移植性,但可能抓不住太多。这取决于编译器设置和环境。



使用默认的VC ++设置,异步(SEH)错误不会传递到C ++ EH基础结构;要捕获它们,您需要使用SEH处理程序(__try / __除外)。 VC ++允许您通过C ++错误处理来传递SEH错误,这允许捕获(...)捕获SEH错误;这包括内存错误,如空指针取消引用。 详细信息



<然而,在Linux上,Windows使用SEH的许多错误都是通过信号表示的。这些都不会被try / catch抓住;要处理它们,你需要一个信号处理程序。


I'm working on porting a Visual C++ application to GCC (should build on MingW and Linux).

The existing code uses __try { ... } __except(1) { ... } blocks in a few places so that almost nothing (short of maybe out of memory type errors?) would make the program exit without doing some minimal logging.

What are the options for doing something similar with GCC?

Edit: Thanks for the pointer to /EH options in Visual Studio, what I need now is some examples on how to handle signals on Linux. I've found this message from 2002.

What other signals besides SIGFPE and SIGSEVG should I watch out for? (Mostly care about ones that might be raised from me doing something wrong)

Bounty Information: I want my application to be able to self-log as many error conditions as possible before it exits.

What signals might I get and which would generally be impossible to log an error message after? (Out of memory, what else?)

How can I handle exceptions and (most importantly) signals in a portable way that the code at least works the same on Linux and MingW. #ifdef is OK.

The reason I don't just have a wrapper process that logs the failure is that for performance reasons I save writing some data to disk till the last minute, so if something goes wrong I want to make all possible attempts to write the data out before exiting.

解决方案

try { xxx } catch(...) { xxx } would be more portable but might not catch as much. It depends on compiler settings and environments.

Using the default VC++ settings, asynchronous (SEH) errors are not delivered to the C++ EH infrastructure; to catch them you need to use SEH handlers (__try/__except) instead. VC++ allows you to route SEH errors through C++ error-handling, which allows a catch(...) to trap SEH errors; this includes memory errors such as null pointer dereferences. Details.

On Linux, however, many of the errors that Windows uses SEH for are indicated through signals. These are not ever caught by try/catch; to handle them you need a signal handler.

这篇关于可移植地处理C ++中的特殊错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆