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?

  • 1
    $\begingroup$ reference.wolfram.com/language/ref/Check.html $\endgroup$
    – ciao
    May 19, 2021 at 22:21
  • 2
    $\begingroup$ Does this answer your question? How to implement try/catch/end try in Mathematica in the most simple way? $\endgroup$
    – Nasser
    May 19, 2021 at 22:44
  • $\begingroup$ 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. $\endgroup$ May 19, 2021 at 23:50
  • 1
    $\begingroup$ 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. $\endgroup$ May 19, 2021 at 23:54
  • 1
    $\begingroup$ @LeonidShifrin A version of Check that returns immediately upon message generation is resources.wolframcloud.com/FunctionRepository/resources/… $\endgroup$
    – Carl Woll
    May 20, 2021 at 3:30


Browse other questions tagged or ask your own question.