Any way to catch runtime errors in a DLL?

Hello *, 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. Michael

Hello Michael, Wednesday, June 14, 2006, 12:41:59 PM, you wrote:
if a runtime error occurs inside a DLL compiled by ghc (like "irrefutable pattern match failed" or exceptions caused by error),
see setUncaughtExceptionHandler in module Control.Exception and module GHC.ConsoleHandler - _may be_ it is that you need -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

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. Cheers, Simon

I've added this qn and reply to the GHC FAQ http://haskell.org/haskellwiki/GHC/FAQ#The_Foreign_Function_Interface | -----Original Message----- | From: glasgow-haskell-users-bounces@haskell.org [mailto:glasgow-haskell-users-bounces@haskell.org] | On Behalf Of Simon Marlow | Sent: 14 June 2006 11:19 | To: Michael Marte | Cc: GHC Users Mailing List | Subject: Re: Any way to catch runtime errors in a DLL? | | 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. | | Cheers, | Simon | | _______________________________________________ | Glasgow-haskell-users mailing list | Glasgow-haskell-users@haskell.org | http://www.haskell.org/mailman/listinfo/glasgow-haskell-users

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

Hello Michael, Thursday, June 15, 2006, 3:32:12 PM, you wrote:
However, this handler catches IO exceptions only! Following the advice from this thread I changed the handler as follows:
Control.Exception.catch
that is a well-known (for gurus :) problem. Haskell98 supports catching of IO exceptions only and corrresponding 'catch' was even included in Prelude. modern Haskell compilers supports catching of any exceptions but to ensure full H98 compatibility, function to catch all exceptions is exposed under different name, namely Control.Exception.catch. so to use "proper" catching you need to work via functions in these module, either qualifying them or hiding 'catch' from Prelude: import Prelude hiding (catch) import Control.Exception if you still don't read "Tackling the awkward squad: monadic input/output, concurrency, exceptions, and foreign-language calls in Haskell" http://research.microsoft.com/Users/simonpj/papers/marktoberdorf/marktoberdo... - i highly recommend you this paper -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Exceptions generated from Haskell you can catch, but the really bad ones are those that the runtime system itself generates (when something really bad happens, like a failed malloc). I have a set of patches that fixes those, so you can actually write safe DLLs. One of these days I'll file a bug report with them. :) -- Lennart Michael Marte wrote:
Hello *,
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.
Michael _______________________________________________ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
participants (5)
-
Bulat Ziganshin
-
Lennart Augustsson
-
Michael Marte
-
Simon Marlow
-
Simon Peyton-Jones