C++ does not catch exceptions when used with Haskell-main and linked by ghc
Consider the C++ program
```
// Main_cc.cc
#include <iostream>
#include <exception>
extern "C" {
int func() {
try {
throw std::runtime_error("THIS FAILS!");
} catch(const std::runtime_error &c) {
std::cerr << c.what();
}
return 0;
}
}
```
and the Haskell program
```hs
-- Main.hs
module Main where
import Foreign.C.Types
foreign import ccall func :: IO CInt
main :: IO ()
main = print =<< func
```
When compiled with
```
ghc --make Main.hs Main_cc.cc -lstdc++
```
and being run on mac os x 10.11.3 the result is
```
libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: THIS FAILS!
Abort trap: 6
```
and not (as it should be):
```
THIS FAILS!0
```
When compiled in a similar way under windows some c++ exceptions of a larger project are not caught as well. However, these were hard to track down and I was unable to construct a simple example like the one above.
As a workaround one can
- write a main() function in c++ and call from there into Haskell,
- compile the Haskell-Module with the options "-c -no-hs-main", and
- link the application using g++ (not ghc!).
When being compiled / linked this way, all c++ exceptions are being caught in mac os x as well as under windows.
<details><summary>Trac metadata</summary>
| Trac field | Value |
| ---------------------- | ---------------- |
| Version | 7.10.3 |
| Type | Bug |
| TypeOfFailure | OtherFailure |
| Priority | normal |
| Resolution | Unresolved |
| Component | Runtime System |
| Test case | |
| Differential revisions | |
| BlockedBy | |
| Related | |
| Blocking | |
| CC | pl, simonmar |
| Operating system | Unknown/Multiple |
| Architecture | |
</details>
<!-- {"blocked_by":[],"summary":"C++ does not catch exceptions when used with Haskell-main and linked by ghc","status":"New","operating_system":"Unknown/Multiple","component":"Runtime System","related":[],"milestone":"","resolution":"Unresolved","owner":{"tag":"Unowned"},"version":"7.10.3","keywords":["c++","exceptions"],"differentials":[],"test_case":"","architecture":"","cc":["pl","simonmar"],"type":"Bug","description":"Consider the C++ program\r\n{{{\r\n// Main_cc.cc\r\n#include <iostream>\r\n#include <exception>\r\nextern \"C\" {\r\nint func() {\r\n try {\r\n throw std::runtime_error(\"THIS FAILS!\");\r\n } catch(const std::runtime_error &c) {\r\n std::cerr << c.what();\r\n }\r\n return 0;\r\n}\r\n}\r\n}}}\r\nand the Haskell program\r\n{{{#!hs\r\n-- Main.hs\r\nmodule Main where\r\nimport Foreign.C.Types\r\nforeign import ccall func :: IO CInt\r\nmain :: IO ()\r\nmain = print =<< func\r\n}}}\r\nWhen compiled with\r\n{{{\r\nghc --make Main.hs Main_cc.cc -lstdc++\r\n}}}\r\nand being run on mac os x 10.11.3 the result is\r\n{{{\r\nlibc++abi.dylib: terminating with uncaught exception of type std::runtime_error: THIS FAILS!\r\nAbort trap: 6\r\n}}}\r\nand not (as it should be):\r\n{{{\r\nTHIS FAILS!0\r\n}}}\r\nWhen compiled in a similar way under windows some c++ exceptions of a larger project are not caught as well. However, these were hard to track down and I was unable to construct a simple example like the one above.\r\n\r\nAs a workaround one can \r\n * write a main() function in c++ and call from there into Haskell, \r\n * compile the Haskell-Module with the options \"-c -no-hs-main\", and \r\n * link the application using g++ (not ghc!). \r\nWhen being compiled / linked this way, all c++ exceptions are being caught in mac os x as well as under windows.","type_of_failure":"OtherFailure","blocking":[]} -->
issue