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

<p>The existing code uses <code>__try { ... } __except(1) { ... }</code> 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.</p>

<p>What are the options for doing something similar with GCC?</p>

<p>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 <a href="http://gcc.gnu.org/ml/gcc-help/2002-04/msg00003.html" rel="nofollow noreferrer">this message</a> from 2002.</p>

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

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

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

<p>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.</p>

<p>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.</p>

[{'answer_id': 39231, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 0, 'selected': False, 'text': '<p>Why not use the C++ standard exceptions instead of MSFT\'s proprietary extension? C++ has an exception handling concept.</p>\n\n<pre><code>struct my_exception_type : public logic_error {\n    my_exception_type(char const* msg) : logic_error(msg) { }\n};\n\ntry {\n    throw my_exception_type("An error occurred");\n} catch (my_exception_type&amp; ex) {\n    cerr &lt;&lt; ex.what &lt;&lt; endl;\n}\n</code></pre>\n\n<p>C++ also has a “catchall” clause so if you want to log exceptions you can use the following wrapper:</p>\n\n<pre><code>try {\n    // …\n}\ncatch (...) {\n}\n</code></pre>\n\n<p>However, this is not very efficient in C++ because creating such a general wrapper means that handling code has to be inserted in <em>every</em> subsequent stack frame by the compiler (unlike in managed systems like .NET where exception handling comes at no additional cost as long as no exception is actually thrown).</p>\n'}, {'answer_id': 39238, 'author': 'DrPizza', 'author_id': 2131, 'author_profile': 'https://Stackoverflow.com/users/2131', 'pm_score': 5, 'selected': True, 'text': '<p>try { xxx } catch(...) { xxx } would be more portable but might not catch as much.  It depends on compiler settings and environments.</p>\n\n<p>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.  <a href="http://msdn.microsoft.com/en-us/library/1deeycx5.aspx" rel="noreferrer">Details</a>.</p>\n\n<p>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.</p>\n'}, {'answer_id': 46978, 'author': 'Thomas Kammeyer', 'author_id': 4410, 'author_profile': 'https://Stackoverflow.com/users/4410', 'pm_score': 0, 'selected': False, 'text': "<p>For portability, one thing to try is using try-catch blocks for most vanilla exceptions and then set a terminate handler (set_terminate_handler) to have a minimal hook available for catastrophic exit conditions.  You can also try adding something like an atexit or on_exit handler.  Your execution environment may be bizarre or corrupt when you enter these functions, of course, so be careful of how much you presume a sane environment.</p>\n\n<p>Finally, when using regular try-catch pairs you can consider using function try blocks as opposed to opening a try block in the body of a function:</p>\n\n<pre><code>int foo(int x) try {\n  // body of foo\n} catch (...) {\n   // be careful what's done here!\n}\n</code></pre>\n\n<p>they're a relatively unknown chunk of C++ and may in some cases offer recovery even in the event of partial (small scale) stack corruption.</p>\n\n<p>Finally, yes, you'll probably want to investigate which signals you can continuably handle on your own or on which you might abort, and if you want less handling mechanisms in place, you might consider call the none-throwing version of the new operator, and compiling to not generate floating point exceptions if needed (you can always check isnan(.), isfinite(.), on FP results to protect yourself).</p>\n\n<p>On that last note, be careful:  I've notice that the floating point result classification functions can be in different headers under linux and windows... so you may have to conditionalize those includes.</p>\n\n<p>If you're feeling puckish, write it all using setjmp and longjmp (that's a joke...).</p>\n"}, {'answer_id': 1173441, 'author': 'Partial', 'author_id': 127716, 'author_profile': 'https://Stackoverflow.com/users/127716', 'pm_score': -1, 'selected': False, 'text': '<p>One way that is easy to use, portable, and barely use any resources would be to catch empty classes. I know this may sound odd at first, but it can be very useful.</p>\n\n<p>Here is an example I made for another question that applies for your question too: <a href="https://stackoverflow.com/questions/556255/how-to-throw-good-exceptions/1081443#1081443">link</a></p>\n\n<p>Also, you can have more than 1 catch:</p>\n\n<pre><code>try\n{\n   /* code that may throw exceptions */\n}\ncatch (Error1 e1)\n{\n   /* code if Error1 is thrown */\n}\ncatch (Error2 e2)\n{\n   /* code if Error2 is thrown */\n}\ncatch (...)\n{\n   /* any exception that was not expected will be caught here */\n}\n</code></pre>\n'}, {'answer_id': 1179382, 'author': 'Hans Malherbe', 'author_id': 126225, 'author_profile': 'https://Stackoverflow.com/users/126225', 'pm_score': 0, 'selected': False, 'text': '<p>Catching C++ exceptions with <code>catch(...)</code> already puts you in a twilight zone.</p>\n\n<p>Trying to catch errors not caught by <code>catch(...)</code> puts you squarely inside undefined behaviour. No C++ code is guaranteed to work. Your minimal logging code may cause the missile to launch instead.</p>\n\n<p>My recommendation is to not even try to <code>catch(...)</code>. Only catch exceptions that you can meaningfully and safely log and let the OS handle the rest, if any. </p>\n\n<p>Postmortem debugging gets ugly if you have error handling code failures on top of the root cause.</p>\n'}]
['https://Stackoverflow.com/questions/39222', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/163/']