Handle exceptional errors in C ++

I am working on porting a Visual C ++ application to GCC (should be built on MingW and Linux).

Existing code uses __try { ... } __except(1) { ... }blocks in several places, so almost nothing (perhaps due to errors like memory?) Will lead to the exit of the program without minimal logging.

What are the options for doing something similar with GCC?

Edit: thanks for the pointer to the / EH options in Visual Studio, now I need some examples of how to handle signals in Linux. I have found this post since 2002.

What other signals besides SIGFPEand SIGSEVGshould be monitored? (Basically caring for those that could be raised from me, something is wrong)

Bounty Information : I want my application to be able to log as many errors as possible before it comes out.

What signals can I receive, and it is usually not possible to register an error message after? (From memory, what else?)

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

The reason I have more than just a wrapper process that registers a failure is because for performance reasons I keep writing data to disk until the last minute, so if something goes wrong, I want to make every possible attempt to write data before exiting.

+5
5

try {xxx} catch (...) {xxx} , . .

V++ , (SEH) ++ EH; , SEH (__try/__ except). V++ SEH ++, catch (...) SEH; , . .

Linux, , , Windows SEH, . try/catch; .

+10

++ MSFT? ++ .

struct my_exception_type : public logic_error {
    my_exception_type(char const* msg) : logic_error(msg) { }
};

try {
    throw my_exception_type("An error occurred");
} catch (my_exception_type& ex) {
    cerr << ex.what << endl;
}

++ "catchall", , , :

try {
    // …
}
catch (...) {
}

++, , ( , .NET, , ).

0

try-catch , (set_terminate_handler), , . - atexit on_exit. , , , , .

, try-catch try try, try :

int foo(int x) try {
  // body of foo
} catch (...) {
   // be careful what done here!
}

++ () .

, , , , , , , , , , , ( isnan (.), isfinite (.), FP, ).

: , linux windows... .

puckish, setjmp longjmp ( ...).

0

++ catch(...) .

, catch(...), undefined. ++ . .

- catch(...). , , .

postmortem , .

0

One way that is easy to use, portable, and barely uses any resources is to catch empty classes. I know that at first this may seem strange, but it can be very useful.

Here is an example that I made for another question that applies to your question too: link

In addition, you may have more than 1 catch:

try
{
   /* code that may throw exceptions */
}
catch (Error1 e1)
{
   /* code if Error1 is thrown */
}
catch (Error2 e2)
{
   /* code if Error2 is thrown */
}
catch (...)
{
   /* any exception that was not expected will be caught here */
}
-1
source

All Articles