0

I'm creating a C++ DLL to be used in C#. This DLL throws exceptions whenever anything is wrong and the idea is to handle these in the C# code.

I created a new exception class in C++ inheriting from std::runtime_error because I not only need the what but also a numeric ID:

class insight_exception: public std::runtime_error
{
public:
    explicit insight_exception(const int id, const std::string message):
    std::runtime_error(message), id{id}
    {}

    virtual ~insight_exception() noexcept {}

    const int id;
};

Whenever something goes wrong in the C++ code I do this:

throw insight_exception(103, "size Pointer is NULL");

I have a little example in C# to "exercise" the DLL, just to test it before sending it to our C# developer, and when I execute the little C# program I can verify that the DLL is throwing the exceptions because I'm getting this:

terminate called after throwing an instance of 'insight_exception'

My problem is that I don't know any C# and I don't really know to "import" the insight_exception class into C# so I can catch it.

Solutions like this post don't help because they assume you can use the class by using a function like this:

insight_exception* create_insight_exception()
{
    return new insight_exception();
}

And I can't do that because I need something like this in C#

try
{
}
catch (insight_exception e)
{
}

So I can't create the class like:

IntPtr insight_exception = create_insight_exception();

One important thing to know is that I'm creating the DLL on Linux by cross-compiling using MinGW, so I can't do #include <Windows.h> or any other windows-related includes or imports when creating the DLL. I don't really use Windows but only for my little test C# code.


EDIT:

Thanks to a comment, I looked into the following questions:

C# not catching unhandled exceptions from unmanaged C++ dll

This one looks promising but the problem is that the answer suggest a compilation done in Windows. I tried to add the /EHa compilation flag equivalent in GCC (-funwind-tables) but that doesn't help. I still can't catch the exception using catch (SEHException ex) nor catch (Exception ex) in the C# code.


Can you catch a native exception in C# code?

Suggests using Win32Exception but that doesn't work either. I can't catch the exception with catch (Win32Exception ex) in the C# code.

m4l490n
  • 1,592
  • 2
  • 25
  • 46
  • `catch(SEHException ex)`? – Charlieface Jan 12 '21 at 22:39
  • @Charlieface and then what? That doesn't seem to do anything. Also, the code doesn't go to the catch, it just crashes. – m4l490n Jan 12 '21 at 22:46
  • Do any of these answer your question? [C# not catching unhandled exceptions from unmanaged C++ dll](https://stackoverflow.com/q/4223470), [Can you catch a native exception in C# code?](https://stackoverflow.com/q/150544), [Catching native C++ exceptions in C#](https://stackoverflow.com/q/11948651), [How to handle exceptions from native code?](https://stackoverflow.com/q/40468923) – greenjaed Jan 12 '21 at 22:55
  • Can you catch any Exception `catch(Exception ex)` – Charlieface Jan 12 '21 at 22:56
  • @Charlieface no, that doesn't work either. – m4l490n Jan 12 '21 at 23:04
  • Sorry, I initially missed the point about the cross compiling... Most probably: **NO**, you cannot catch this exceptions. https://stackoverflow.com/a/5108118/1810087 – user1810087 Jan 12 '21 at 23:39
  • And just `catch { }`? – Charlieface Jan 12 '21 at 23:43
  • @Charlieface no, I still can't with `catch {}`. I think I will have to use return codes instead. I just checked the link `user1810087` mentioned and I see the problem. – m4l490n Jan 12 '21 at 23:46
  • @user1810087 ok, I see. I think I will have to use return codes instead. – m4l490n Jan 12 '21 at 23:46
  • Exceptions are not covered by the standard and depend on the implementation. Different vendors use different implementation which will not match. So `catch { }` should not work, and if it does by some luck, don't depend on it... https://stackoverflow.com/a/491120/1810087 – user1810087 Jan 12 '21 at 23:47
  • @user1810087 ok, that makes sense. It really seems not possible because of the way exceptions work. Well, I will then use a simple return code. – m4l490n Jan 12 '21 at 23:50
  • Another option than the error codes would be using MSVC for your C++ part and C++/CLI: https://stackoverflow.com/a/56965916/1810087 – user1810087 Jan 12 '21 at 23:52
  • C++ stack unwinding is an arcane art full of platform specific hacks. IMHO, write a C++ stub to catch the exception and return some kind of success / failure object. – Jeremy Lakeman Jan 12 '21 at 23:57
  • @user1810087 though that is a good solution, it is not possible because I would have to create a "3rd piece of code" which is the CLI-wrapper and nobody will like that, especially because the exceptions are not required and I can solve easily with a return value and not introducing more source files and code. – m4l490n Jan 12 '21 at 23:57

0 Answers0