Any way to catch runtime errors in a DLL?

Michael Marte marte at pms.informatik.uni-muenchen.de
Thu Jun 15 07:32:12 EDT 2006


Simon Marlow wrote:
> Michael Marte wrote:
> 
>> if a runtime error occurs inside a DLL compiled by ghc (like 
>> "irrefutable pattern match failed" or exceptions caused by error),
>> the application that called the DLL function dies. This is ok for 
>> development but unacceptable when it happens with a user sitting in 
>> front of the display. (It has not yet happened but it's only a 
>> question of time.)
>>
>> So my question is: Is there any way to catch and process runtime 
>> errors? I am looking for some way to map those errors to exceptions on 
>> the C++ side that can be caught if required. It would be ok to kill 
>> the Haskell runtime system and unload the DLL if necessary.
> 
> 
> The FFI doesn't provide any way to propagate exceptions from Haskell to 
> the caller of a foreign export, because there's no standard way to do 
> this.  It is your responsibility to catch the exception in Haskell and 
> return an appropriate error code to the caller.  To raise a C++ 
> exception, you would probably need another C++ wrapper around each 
> foreign export, translating an error code into the C++ exception.
> 

I think my intial problem description should have mentioned that I 
already have an exception handler:

     catch
     (do
        let result = ...
        write result to file
        return 1)
     (\_ -> return 0)

However, this handler catches IO exceptions only!
Following the advice from this thread I changed the handler as follows:

     Control.Exception.catch
     (do
        result <- Control.Exception.evaluate (...)
        write result to file
        return 1)
     (\_ -> return 0)

This handler actually catches all exceptions. I found that it is 
important to use Control.Exception.catch, just catch does not work.
I do not yet know whether I have to use evaluate.

Next thing is to pass the exception messages to the C++ side and I think 
I know how to achieve this.

Michael


More information about the Glasgow-haskell-users mailing list