Distinguishing different exceptions from C# plugin

Hello,

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:

try
    Plugin.CallMethod;
except
  on E: MyPluginException do
    Process(E);
end;

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?

Regards

Hello

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

AppDomain.CurrentDomain.UnhandledException

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

Regards

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)
{
    try
    {
        Proc();
        return 0;
    }
    catch (Exception e)
    {
        setLastErrorDetails(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)
    {
        builder.AppendLine(exception.Message);
        builder.AppendLine(exception.StackTrace);

        exception = exception.InnerException;
    }

    lastErrorDetails = builder.ToString();
}

public string GetLastErrorDetails()
{
    return lastErrorDetails;
}
#endregion

Here is the code that initializes the list of exceptions:

knownExceptionTypes =
    new Type[] {
        typeof(FirstException),
        typeof(SecondException)
    };

And a usage sample:

public int MyMethod(string Parameter)
{
    return HandleExceptions(
        () =>
        {
            DoSomethingWith(Parameter);
        }
    );
}

Hope this helps.