Using GH_ActiveObject.AddRuntimeException() in Custom Class

Hi there!

This is more a question about best practices than an actual problem, but here it goes:

I have created a custom class that will be used inside a custom GH_Component. I have copied a made-up version of the class at the bottom of the message to make my point.

What I want to do is being able to call AddRuntimeException(), to throw an alert if the Surface.ClosestPoint()method did not succeed. Since AddRuntimeException() is only accesible from the actual component, I have created a new variable in my class to hold a reference to it’s owner (the component that initialized it).

So, my question is as follows:

  1. Is this an actual good idea?
  2. Is there an better way to do this?

Since my end goal is to create a family of components that talk to each other, and I would be passing around this custom classes:

  1. Should I be deriving them from IGH_Goo and creating GH_Param wrappers for all the classes I would be passing around?
  2. Would deriving from IGH_Goo give me access to throwing exceptions?

I guess they would be only 2 or 3 classes that I would need to pass around so it wouldn’t be a lot of work… Just asking!

This is the class:

    public class ChebychevAxis
        Surface _surface;
        double u, v;
        ChebychevNetGHComponent owner;
        DataTree<Point3d> _axisPoints;

        public ChebychevAxis(Surface surface, Point3d startPoint)
            _surface = surface;
            bool success = _surface.ClosestPoint(startPoint, out u, out v);
            if (!success) owner.AddRuntimeMessage(Grasshopper.Kernel.GH_RuntimeMessageLevel.Error, "OOPS!! Something happend while setting the starting point");

Thanks in advance!

P.D. Markdown text is fun :slight_smile:

hmm… I guess my post got lost in the ocean :frowning:

What is the purpose of your class? Is it a container of data or functionality? or both? Given the sample, it looks like functionality. Put the functionality in a method (instead of in the constructor) that returns a boolean that says whether it was worked correctly (or an integer for several kind of exceptions or a custom struct to send the detailed exception). That way inside the SolveInstance you call the method and if it returns false you launch the exception. This is how Rhino works, much of its functions return a boolean that indicates whether it has run correctly.

If you need a data type, yes, but use GH_Goo< T> instead of IGH_Goo.

To launch the exception you need the component, and if there is a way to access it from the data (I doubt it) it will be a pretty dirty way to do it.

I think the most understandable way to do this is as I said at the beginning. You need to use the classes from the component in SolveInstance, so you just need to modularize it so that the context that uses those classes (SolveInstance) knows if an exception has occurred when you call the method.

1 Like

Hi! Sorry for the late reply and thank you very much for taking your time to reply!

Yes, you are 100% correct: It is a functionality.
I wasn’t very proficient on the use of structs & using them to throw exceptions… so I went to dig into the internet and after some trial and error I think I got the idea.

I will change the return type and once I get it working I will try to post it here for the less experienced in programming :wink:

Again, thank you very much taking your time to answer, it was extremely helpful!