C# make runscript error on purpose

Hi,

Not in visual studio but in c sharp editor in grasshopper, I would like to make error on purpose.

Let say

if(i = 0)
ComponentBecomeRed and display a message

How can I make this?

Assigned to Grasshopper category

Instead of red I would suggest:

try{ some stupid thing }
catch{ Print(“Stupid thing did stupid things”);}

That way you can “trace” N errors

Like this:
for Warning (yellow/orange)

this.Component.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, “Warning message.”);

or for Error (red)

this.Component.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, “Error message.”);

You might want to put

return;

after an error to stop the script.

1 Like

In addition to @Michael_Pryor and @PeterFotiadis suggestions, any exception thrown inside the script will be caught and put into the RuntimeMessage list as an error. So

throw new Exception("You're not allowed to divide by zero.");

would both immediately stop the execution of your script and log an error.

2 Likes

this.Component gave me an error
I used this.AddRuntimeMessage() directly like this:

            this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Warning message.");

If you’re using VS to compile your code, then your code is already inside a Component class. For C# scripts however that is not the case. In scripts you have to access the Component field to get that functionality.

that makes sense! thankyou so much.

in VS you won’t even need the “this.”

wow cool. thanks