How to let python do something regularly when catch error?

I have written a program in python component and I want to use it with the genetic algorithm module. While the program usually meets some unexpected errors. Some of them cannot be fixed perfectly, especially those that come from rhinoscriptsyntax. I tried a lot of times and convince that the program can work great in 90% of conditions, most errors don’t have the fundamental effect of the final result. The only thing they do is just stop the process of the genetic algorithm module and let the whole work break.

So I want to achieve the effect as follow:
The component would return regular and pre-prepared information or geometry when it catches any error.

I have prepared a simple python component that would stop when the random index that points out the int-type object but not the point3d object as the first parameter of the AddCirCle method. What I want to do is that the program should return a regular thing like a line or a string when catching TypeError, and skip the wrong code and end the execution directly
image

I hope anyone can help me to solve the problem. I’ve been on this issue for too long😥.

The gh document file is here randomly_return_error.gh (4.2 KB)

Here are the codes if it is not convenient for you to download the attachment.

import rhinoscriptsyntax as rs
import random

if x == True:
    pt = rs.coerce3dpoint([0, 10, 0])
    num = 3
    rad = 5
    ctls = (pt, num)
    
    indexA = random.randint(0, 1)
    a = rs.AddCircle(ctls[indexA], rad)

You could for instance use a try except statement.

import rhinoscriptsyntax as rs
import random

if x == True:
    pt = rs.coerce3dpoint([0, 10, 0])
    num = 3
    rad = 5
    ctls = (pt, num)
    
    indexA = random.randint(0, 1)
    try:
        a = rs.AddCircle(ctls[indexA], rad)
    except ValueError as e:
        raise(e)

The code inside the try clause is executed as long as no exceptions occur, meaning that no errors occur in the executing code.
If something goes wrong the except clause is called. It is common in Python to indicate which exceptions you want to catch with except and that’s why I put ValueError. If you don’t put anything or simply Exception, it will catch all exceptions, which might be good or bad, depending on your case. In my opinion, it’s better to be specific.
Inside the except clause, you can then stop the code execution or do anything you want really. I simply throw the exception e that was previously raised by rs.AddCircle. This stops the code execution.

You could also do something like this, if you don’t want to raise an exception, but only selectively run code after one has occurred:

import rhinoscriptsyntax as rs
import random

if x == True:
    pt = rs.coerce3dpoint([0, 10, 0])
    num = 3
    rad = 5
    ctls = (pt, num)
    
    indexA = random.randint(0, 1)
    cir = None
    try:
        cir = rs.AddCircle(ctls[indexA], rad)
    except ValueError as e:
        cir = rs.AddCircle([0,0,0], rad)
   
    a = cir

There are really many ways to do this. It always depends on your specific case though.
Here’s another example without try except:

import Rhino.Geometry as rg
import random


def generate_circle(center, radius):
    """Returns a circle, if center is a valid three-dimensional point."""
    if not isinstance(center, rg.Point3d):
        return
    return rg.Circle(center, radius)


if x == True:
    pt = rg.Point3d(0, 10, 0)
    num = 3
    rad = 5
    ctls = (pt, num)
    
    indexA = random.randint(0, 1)
    cir = generate_circle(ctls[indexA], rad)
    
    a = cir if cir else rg.Circle(rg.Point3d.Origin, rad)
1 Like

Thank you! I’m not very familiar with the consistency of exception handling. Your explanation helped me solve my doubts. :smiling_face_with_three_hearts: