Learning scripting

I am trying to learn more about scripting. I downloaded this sample script, and it has an error when i enter 0. Why isn’t it printing something went wrong and can someone fix it?

2_1_SomeNumber.py (220 Bytes)

Edit: I didn’t read properly sorry.

That okay. I just wonder why a premade sample script isn’t working.

try this:

import rhinoscriptsyntax as rs
somenumber = rs.GetReal("Line length")
try:
    line = rs.AddLine( (0,0,0), (somenumber,0,0) )
except Exception as ex:
    print (ex)
    print("Something went wrong")
else:
    print("Line curve inserted with id", line)

If we look up the definition of AddLine, we can see the error message that is coming up

def AddLine(start, end):
    """Adds a line curve to the current model.
    Parameters:
      start, end (point|guid) end points of the line
    Returns:
      guid: id of the new curve object
    Example:
      import rhinoscriptsyntax as rs
      start = rs.GetPoint("Start of line")
      if start:
          end = rs.GetPoint("End of line")
          if end: rs.AddLine(start, end)
    See Also:
      CurveEndPoint
      CurveStartPoint
      IsLine
    """
    start = rhutil.coerce3dpoint(start, True)
    end = rhutil.coerce3dpoint(end, True)
    rc = scriptcontext.doc.Objects.AddLine(start, end)
    if rc==System.Guid.Empty: raise Exception("Unable to add line to document")
    scriptcontext.doc.Views.Redraw()
    return rc

Which would mean you would need to have a try … catch block to stop the exception from breaking the script, which seems a bit complex for introductory scripting
https://developer.rhino3d.com/guides/rhinopython/primer-101/2-python-essentials/#21-language-origin

@stevebaer might be able to shed some light?

That worked thanks

Thanks