Distinguishing different exceptions from C# plugin


I’m using Hydra to access C# non visual code from a Delphi host application and it works just fine.
However, on some occasions, the C# code can throw exceptions that inherit from a common base type, say MyPluginException.
On the Delphi side, I would like to distinguish between those exceptions like so:

  on E: MyPluginException do

So that, exceptions that derive from MyPluginException are processed while any other exception is left untouched.
However, right now, I’m not getting any specific type for these exceptions, they all are raised as EOleException without any details.
I mean, sure, there is the message, but I’m missing the source C# exception name and its associated stack trace. The former would allow me to do a switch while the latter would allow me to give more details to our support team (we are using madExcept for the Delphi side, for instance).
Is this something that is feasible? If yes, how would you suggest that I do it?



Well, plugin and host don’t know anything about each other’s types, so plugin just cannot raise MyPluginException exception.

The easies way to get the information you need would be to includ it into the exception message. Other way is to add a handler for event


There you can save all the exception data you need (ie message, type name, stacktrace etc).

Then add a method GetLastError to the plugin interface and return via it the exception information.

Then on the host side in case of an exception call this method to get detailed info


Ok, that’s what I thought of also, so I’ll go this route, quite similar to other APIs I’m working with.

For SDK it is free in terms of performance and code complexity (I mean user code here). Here it would need an additional layer of abstraction ho handle a situation (exception paasing from plugin to the host) that should be a corner-case.

However I will log an issue to discuss if we should add the GetLastError method as a part of built-in functionality in Hydra.

Thanks for this.
Here what I am using now:

#region Exception handling
private string lastErrorDetails;
private Type[] knownExceptionTypes;

private int HandleExceptions(Action Proc)
        return 0;
    catch (Exception e)
        if (knownExceptionTypes != null)
            for (int i = 0; i < knownExceptionTypes.Length; i++)
                if (knownExceptionTypes[i] == e.GetType())
                    return i + 1;
        return -1;

private void setLastErrorDetails(Exception exception)
    StringBuilder builder = new StringBuilder();
    while (exception != null)

        exception = exception.InnerException;

    lastErrorDetails = builder.ToString();

public string GetLastErrorDetails()
    return lastErrorDetails;

Here is the code that initializes the list of exceptions:

knownExceptionTypes =
    new Type[] {

And a usage sample:

public int MyMethod(string Parameter)
    return HandleExceptions(
        () =>

Hope this helps.