How can I change Error messages in python?

Viewed 48

This is the code:

    A = np.array([ [x1**2, x1, z1], [x2**2, x2, z2], [x3**2, x3, z3] ])
    B = np.array([y1, y2, y3])
    B = B.T

    global X
    try:
        X = linalg.solve(A, B)
    except TracebackError:
        raise InputValueError('Input values nopt compatible') from None

So basically I want to have this euqation solved (by scipy). If I have two of the same input tho, I get a Traceback Error. to make it more clear and clean, if the Traceback error accures, I want to have it changed to "InputValueError('Input values not compatible')". How exactly does this try/except work and how do I use it correctly?

p.s.: The code after "try" is obviously wrong and that's the part where I need your help

1 Answers

There is no need for the from None part

try:
     X = linalg.solve(A, B)
except np.linalg.LinAlgError as e:
     print("Got: ", e)

     raise InputValueError('Input values nopt compatible')
Related