Ask Your Question
1

Error Handling

asked 2022-01-30 20:06:57 +0100

Rune gravatar image

I am very new to Sage, so apologies if the answer to this question is obvious.

I have a program, and at the final calculation, for T and E matrices calculated earlier, it needs to either calculate a matrix A such that T=AE, or, if this returns an error message, calculate a matrix A such that p*T=AE, where p is also calculated earlier.

In mock code, what I mean is:

Calculate E.solve_left(T)
If that returns an error:
      A=E.solve_left(p*T)
else:
      A=E.solve_left(T)
return A

But I don't know how to do that in real code.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2022-01-30 20:16:03 +0100

Max Alekseyev gravatar image

updated 2022-01-31 18:32:38 +0100

You need to handle an exception (this is Python feature inherited in Sage):

try:
    A=E.solve_left(T)
except:
    A=E.solve_left(p*T)
edit flag offensive delete link more

Comments

Or simply:

try:
    A = E.solve_left(T)
except:
    A = E.solve_left(p*T)

Note that it is better to name the exception you expect to catch (for example except ValueError) to avoid catching unexpected errors.

Sébastien gravatar imageSébastien ( 2022-01-31 17:51:47 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-01-30 20:06:57 +0100

Seen: 543 times

Last updated: Jan 31 '22