1

In Python, we have the command try in Python that allows to run a command, and then if an error occurs, the except block kicks in.

What does Mathematica have, which may be similar?

An old man in the sea.
  • 2,527
  • 1
  • 18
  • 26
  • 1
    https://reference.wolfram.com/language/ref/Check.html – ciao May 19 '21 at 22:21
  • 2
  • To the closers: I don't think there is a single direct answer to this question, for a number of more subtle reasons. At least, neither of the links mentioned in comments above, provide the answer, IMO. There is also this canonical discussion, which is somewhat related, particularly if one is also interested in an analog of finally clause. Surely, one can produce an answer by combining some of the ingredients described in various Q/A-s scattered on the site, but I don't believe it has been done. – Leonid Shifrin May 19 '21 at 23:50
  • 1
    The problem with Check is that it allows the full execution of the first expression to its end, even if the message has been generated early on. To the extent that this is acceptable, the answers in the linked Q / A - s are satisfactory, but this is not the behavior you see in Python and other languages with exceptions, where the execution is stopped right away. I think, this question is a good opportunity for some of the potential answers, to highlight the difference between message-based and exception-based error-handling, and associated subtle points. – Leonid Shifrin May 19 '21 at 23:54
  • 1
    @LeonidShifrin A version of Check that returns immediately upon message generation is https://resources.wolframcloud.com/FunctionRepository/resources/CheckReturn – Carl Woll May 20 '21 at 03:30
  • @CarlWoll Thanks, that's good to know. I knew some of the main ingredients, but your implementation is instructive, and some things were new to me. My point in comments here was though not that such a version is impossible to obtain, but that Check is not it, and all existing / linked answers overlook this issue, and can not be considered exact analogues of try / except in Python. – Leonid Shifrin May 20 '21 at 04:06

0 Answers0