views:

153

answers:

5

Hello, I'm writing a C++ application. I realized that one of my worker threads may terminate unexpectedly. The (VS 2005) debug log says:

The thread 'Win32 Thread' (0x5d98) has exited with code -858993460 (0xcccccccc).

I surrounded all the worker thread code with a try/catch block. So, if the reason was an exception, I would catch it. But I can't:

try{
   ...
   Connection* conn = connectionPool->getConnection(); // unexpected exit occurs here
   ...
} catch(exception& e) {
   ...
}

I have ten threads running concurrently, and only one of them gets crashed after some time, while the others continue running (and getting new [OCCI] connections).

Is there an exception type that is not caught by "exception"? Or what do I not know about threads/exceptions?

Thanks.

+2  A: 

Not all errors raise C++ exceptions that can be caught with the C++ try...catch mechanism. For example, division by zero will not raise a C++ exception, but will cause undefined behaviour which may well make your application exit.

However, your code may be throwing things that are not derived from std::exception , so you may want to rewrite:

try{
   Connection* conn = connectionPool->getConnection(); // unexpected exit occurs here
} 
catch(exception& e) {
    // handle things derived from std::exception
}
catch ( ... ) {
   // handle things that are not so derived
}

which will deal with things like throw "eeek!";

Also, but nothing to do with the problem, you should normally catch exceptins via a const reference.

anon
How can I catch this kind of unexpected exceptions than? So my thread won't exit without my knowledge.
sahs
@sahs In general, you can't - you have to write your code so that such errors do not occur. Exceptions are not meant to be a band-aid to apply to poor code.
anon
@Neil I'm using OCCI library to get a connection, and that's where the exception occurs. All other threads can get connection while one of them exits unexpectedly. Anyway, your suggestion will probably solve the problem. I'm trying it now, thanks.
sahs
It didn't solve :(. Still the thread doesn't catch any exceptions. Now it's time to try what PiotrLegnica says.
sahs
+1  A: 

The easiest way to find the problem would be to run your application under a debugger and enable breaking on Win32 Exceptions. Whenever a Win32 Exception is encountered, the application would break into the debugger and you can find out whats going wrong.

If you are not debugging and want to catch a win32 structured exception, you have to use _set_se_translator api to set a translator function. The registered function will be called whenever there is a Win32 exception and you get a chance to convert it to a C++ exception of your choice.

Canopus
Right, but does it break if a thread throws the exception? As far as I see, it doesn't..?
sahs
Yes it would if its a Win32 Exception. If it is a C++ exeception you will have to enable C++ exceptions as well.
Canopus
@Canopus I just checked the debugger settings(VS2005), all the "user-unhandled" exception types break the application already. Do you thing it means that the thread does not terminate because of an exception? What can be other reasons to terminate?
sahs
If you are familiar with windbg, try running your application attached to it. In windbg you may enable breaking exection on Exit Thread event.
Canopus
+1  A: 

Is there an exception type that is not caught by "exception"?

Yes, SEH exceptions. To catch them you need to either use __try/__except MSVC extension (see Structured Exception Handling), or write a global SEH/VEH handler (see SetUnhandledExceptionFilter and AddVectoredExceptionHandler).

PiotrLegnica
Are you sure? The first link you gave says: "The C++ exception handling mechanism is more flexible, in that it can handle exceptions of any type."
sahs
@sahs: that applies to custom code. Windows reports its exceptions (like division by zero, or access violation) using SEH. They can be caught with `catch(...)` if you compile with proper [`/EH`](http://msdn.microsoft.com/en-us/library/1deeycx5%28VS.90%29.aspx), though. To use normal `catch`, you need to translate them into C++ exceptions first, as mentioned in another answer.
PiotrLegnica
+1  A: 

catch(exception& e) catches C++ exceptions derived from std::exception, and nothing else.

It doesn't catch C++ exceptions that are not derived from that class (If I do throw 42, it won't be caught for example), and it doesn't catch exceptions or errors at the system level.

Windows uses Structured Exception Handling (SEH) to signal errors, and those are not caught with a plain C++ catch statement. This might include errors like division by zero, as well as access violations or pretty much anything else that might go wrong at the OS or hardware level.

The docs have a nice explanation of how to catch SEH exceptions.

jalf
A: 

I found an important clue. When you close a handle with CloseHandle function, the thread exits with code 0xCCCCCCCC. With the help of this clue, I realized that in a very rare situation, I close my thread's handle even though the thread's working. Why does it exit exactly while getting connection? That also has an explanation, but it's related to the structure of the code, which may be difficult to explain here.

Thank you all, who brainstormed with me on the exceptions issue :$.

sahs