Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching exceptions thrown from DLLs

Tags:

c++

exception

dll

When an exception exits a function in a DLL the mingw32 runtime simply calls terminate std::unexpected instead of propagating the exception to the code that is calling the DLL. What solutions are there to this problem? The DLL and the application calling it are both compiled with the same compiler.

There are two different exception mechanisms supported by mingw32: SJLJ and Dwarf2. Should one of them work better than the other for this? Perhaps the only option is to switch to MSVC or ICC or maybe changing build options alone would help?

Notice that not even catch(...) will catch any exception, not even built-in types (throw 1;), so it is not about the visibility of the exception type.

like image 667
Tronic Avatar asked Nov 09 '10 13:11

Tronic


People also ask

Can you catch an exception throw?

When an exception is cached in a catch block, you can re-throw it using the throw keyword (which is used to throw the exception objects). Or, wrap it within a new exception and throw it.

Does catching exception stop execution?

Answer: When an exception is thrown in the catch block, then the program will stop the execution.

What is the difference between catch and throw exception?

Try-catch block is used to handle the exception. In a try block, we write the code which may throw an exception and in catch block we write code to handle that exception. Throw keyword is used to explicitly throw an exception. Generally, throw keyword is used to throw user defined exceptions.

How do you handle exceptions without catching?

throws: The throws keyword is used for exception handling without try & catch block. It specifies the exceptions that a method can throw to the caller and does not handle itself.


1 Answers

Is the runtime assuming that extern "C" functions will never throw exceptions? I'm not familiar with MinGW but I know that Visual Studio has a bunch of command line arguments to control this sort of behavior. For example, the /EHs option will cause it to assume that extern "C" will never throw and it will treat functions that do throw by calling std::unexpected() which in turn calls std::terminate(). You might want to call std::set_unexpected() to establish an unexpected exception handler and see if it traps.

like image 80
D.Shawley Avatar answered Sep 28 '22 04:09

D.Shawley