Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Portably handle exceptional errors in C++

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.

like image 615
thelsdj Avatar asked Sep 02 '08 10:09

thelsdj


People also ask

How do you handle exception in C?

As such, C programming does not provide direct support for error handling but being a system programming language, it provides you access at lower level in the form of return values. Most of the C or even Unix function calls return -1 or NULL in case of any error and set an error code errno.

Can we handle exceptions in C?

The C programming language does not support exception handling nor error handling.

What are exceptions in C?

Master C and Embedded C Programming- Learn as you go A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero. Exceptions provide a way to transfer control from one part of a program to another.

How many types of exception handling are there in C?

There are two types of exceptions: a)Synchronous, b)Asynchronous (i.e., exceptions which are beyond the program's control, such as disc failure, keyboard interrupts etc.). C++ provides the following specialized keywords for this purpose: try: Represents a block of code that can throw an exception.

What are the types of errors in exception handling?

There are three types of errors in programming: (a) Syntax Errors, (b) Runtime Errors, and (c) Logical Errors.


1 Answers

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.

like image 142
DrPizza Avatar answered Nov 13 '22 22:11

DrPizza