NodeModel node with button crash: InvalidOperationException possible in NodeModel button nodes?

Hi guys!

I’ve got a question about NodeModel buttons.

image

I created a button with a Button_Click event in the *.xaml:

<Button Click="Button_Click" Content="Do Stuff :)"/>

And then created the Button_Click method in the *.xaml.cs:

private void Button_Click(object sender, RoutedEventArgs e)
{
   ...
   if (some check)
        {
            throw new System.InvalidOperationException("error message in Dynamo");
        }
   ...
}

All functions under the button’s code are working well, except with System.InvalidOperationException(), where Dynamo mysteriously crashes. I get a Dynamo crash report but it’s not really informative. It basically points to where it went wrong in my code (which is at the throw new … line).

image

Does anyone know why? Is it because I’m not implementing the button correctly? Should I rather use a binding? If so, does anyone have a nice example?

Again, many thanks for your help in advance :smiley:

Michael

it’s crashing because you’re throwing an exception which is not caught.
Exceptions are only caught and shown as errors if they occur during node execution - your button click callback handler is not happening as part of node execution.

Had to think about it for a bit but that does make sense.

I guess it’s probably best for now to limit the use of buttons on nodes then, as it’s not part of running graphs.

Perhaps I can put this functionality in a ViewExtension.

Thanks again!