RhinoScript – Rhino.Exit doesn't work in an IF statement


I am trying to write a Rhinoscipt program to run from the Windows command prompt that closes Rhino when it is complete. At the end of my code, I have been successfully using Rhino.Exit; however, it doesn’t work when I try to use it to abort the script in case of an error.

The script below attempts to create geometry using a parameter-driven Grasshopper file. It then checks if any geometry exists, writing the current state to a flag and exiting if necessary. I have tested the code and found that the flag is correctly written as “0” if there is no geometry; but then why doesn’t Rhino.Exit run?

Any help would be greatly appreciated.


Call Main()
Sub Main()
        ' Auto-bake a Grasshopper file that can fail to generate geometry under certain conditions
        ' (This is not an issue with the file and cannot always be determined in advance)

        ' Check that geometry was created
        Dim objects
        objects = Rhino.AllObjects(False,True)
        ' Get the file system object
        Dim objFSO, objStream
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        ' Open a text file to write to
        Set objStream = objFSO.CreateTextFile("GHflag.txt", True)
        If Err Then
                MsgBox Err.Description
                Exit Sub
        End If
        ' Print flag file
        If IsArray(objects) Then                ' Print Success
        Else                                            ' Print Fail
        End If
        ' Close the file
        Set objStream = Nothing
        Set objFSO = Nothing
        ' Do other stuff
        ' ...
        ' Exit Rhino
End Sub


Did you manage to get it to work? I’ve been using rhinoscriptsyntax.Exit() which seems to work consistently. There is a relevant discussion on here: Exit rhino from script/command file

I’ll try that instead next time.

In this case, I just put the rest of the code in an ‘if’ statement, since Rhino.Exit seems to work correctly outside of it.

Okay. Also, I’m not sure if you can use what I suggested because you’re using Rhinoscript and not Python. In any case, you might want to check out the discussion in that other thread.

In case of an error the Rhino.Exit statement will not be executed.
The script engine will simply exit the subroutine.

1 Like