TestErrorCheck - Printing exception info, stack trace

Hi All,

I was experimenting with the TestErrorCheck but cannot seem to find a way to print .NET exceptions to the Rhino command window.

My goal is to get a detailed textual representation of exceptions to test my RhinoCommon plug-in. I need to test it outside of Visual Studio, so inspecting exceptions there is not an option. Another solution that i see is to surround all my custom commands in try/catch blocks, but that seems rather cumbersome.

I will appreciate any suggestions!

Cheers,

Lucas.

Try if you can do something with Rhino.Runtime.HostUtils.ExceptionReport() and OnExceptionReport. You still need to catch the exception though.

What we did was to redirect the command flow in an abstract base class that handles exceptions, and calls RunActualCommand.

public abstract class ExceptCommand : Command
{
    // the concrete command should implement this
    public abstract Result RunActualCommand(RhinoDoc doc, RunMode mode);
    // make this sealed so that the concrete command can no longer implement this
    protected sealed override Result RunCommand(RhinoDoc doc, RunMode mode)
    { 
        try { 
            return RunActualCommand(doc, mode);
        } catch(Exception e)
        {
            //report your exception details here
        }
    }
}

// the concrete command
public class MyCommand : ExceptCommand
{
    public override Result RunActualCommand(RhinoDoc doc, RunMode mode)
    {
        // your command goes here, any exceptions are handled by the base class
    }
}

Thanks a lot for the tip! I didn’t think of checking the Rhino.Runtime.HostUtils class documentation.

FYI: the problem is easily solved by registering your own delegate function with the OnExceptionReport event. For example, make a method or anonymous function:

public void ExceptionReporter(string source, Exception exc)
        {
            string msg = RecordErrorLog ? "This error was written to the IDS error log" : "";
            RhinoApp.WriteLine("Exception occurred in {0}:\n{1}\n{2}", source, exc.ToString(), msg);

            // Write to our error log
            if (RecordErrorLog)
            {
                _error_logger.AppendFormat("===== Exception occurred in {0}. Details below. =====\n{1}\n", source, exc.ToString());
            }
        }

and registering it, e.g. in your plugin constructor or OnLoad() callback as follows:

Rhino.Runtime.HostUtils.OnExceptionReport += ExceptionReporter;

AFAIK, you don’t need to wrap anything in try/catch anymore, except of course if you want to handle your exceptions in some way.

Cheers,

Lucas.