Rhino Common Error Handling


I was wondering how you guys handle Errors in C#.net Development for Rhino.

For example:

This code adds a circle to the Document in RhinoPython

def AddArc(plane, radius, angle_degrees):
    """Adds an arc curve to the document
      plane = plane on which the arc will lie. The origin of the plane will be
        the center point of the arc. x-axis of the plane defines the 0 angle
      radius = radius of the arc
      angle_degrees = interval of arc
      id of the new curve object
    plane = rhutil.coerceplane(plane, True)
    radians = math.radians(angle_degrees)
    arc = Rhino.Geometry.Arc(plane, radius, radians)
    rc = scriptcontext.doc.Objects.AddArc(arc)
    if rc==System.Guid.Empty: raise Exception("Unable to add arc to document")
    return rc

Here is a simllar function in C#

public static Guid AddArc(Plane plane, double radius, double angle, bool angleAsDegrees = true)
            RhinoDoc document = RhinoDoc.ActiveDoc;

            if (angleAsDegrees)
                // set angle to be in radians
                angle = RhinoMath.ToRadians(angle);

            Arc arc = new Arc(plane, radius, angle);

            if (arc == null)
                return System.Guid.Empty;

            Guid guid = document.Objects.AddArc(arc);
            return guid;

It has no Error handling. How would you do that?


I use Visual studio 2010. You can create commands and run them. If it fail´s it will show you where the Error is.
There is an free version of visual studio 2010 that works great with C# and VB.NET. C++ is not supported on the free version.

After the line

Guid guid = document.Objects.AddArc(arc);

you could add the test for Guid.Empty, then throw an exception, like in the VB:

if(guid==Guid.Empty) throw new Exception("Unable to add arc to document");

In the end it is up to the developer to decide how one expects code to behave in case of exceptions. Does one throw an exception, making it the problem of other code, or does one handle an exception in their own code.

One can find lots of discussions on pros and cons of either way, I don’t think there’s really a holy grail - not as long as errors can happen.