# How to implement try/catch/end try in Mathematica in the most simple way?

There are lots of question/answers about exception handling in Mathematica. I still find exception handling in Mathematica too complicated to work with.

I find Maple much simpler and easier to understand when it comes to exception handling.

What would be the simplest translation of the following code from Maple to Mathematica?

foo:=proc(x,y)
try
x/y;
catch:
error "I give up, bad input";
end try;
end proc;


The above defines a function foo. The main exception handling is done using try .... catch: ... end try

The call to error terminates the function sends an error message to the caller (if any).

The use of : after catch means to catch ANY error. (otherwise one can catch specific error.

So when calling the above in Maple this is the result

And when calling it as foo(1,1) no error is generated.

I do not want to set separate functions and define external message string and tags and so on in Mathematica to do the above as shown in Error checking and trapping techniques with Throw and Catch for example which is very good but I find hard to follow and more complicated than in Maple. I'd like everything done inside the Module

The try .. catch... end try is much simpler to use and understand.

What is the simplest way to implement similar code as the above Maple function?

• I was waiting to see how you would respond to Roman's answer as it seems to me the natural approach, if I understand the question. Since you did not I guess I'll ask, what's wrong with it? – Mr.Wizard Mar 2 at 9:50

You could do

f[x_, y_] := Check[x/y, $Failed] f[1, 0]  During evaluation of Power::infy: Infinite expression 1/0 encountered.$Failed

In this way you can pass the $Failed to the caller and react to it, instead of passing the default of ComplexInfinity (which is useful in its own way). Maybe something like this? f[x_, y_] := Module[{}, Quiet@Check[x/y, Throw["I give up, bad input"]]]; f[1, 0] (* Throw::nocatch : Uncaught Throw[I give up, bad input] returned to top level. *)  • Thanks., but it seems the error do not get cought by caller. i.e. When I tried g[x_, y_] := Module[{},Check[f[x, y], Throw["f[x,y] threw an error !"]]]; I was expecting to see the error f[x,y] threw an error ! but instead got the error I give up, bad input. Which tells me the throw from f[x,y] goes directly to top level bypassing the caller. – Nasser Feb 29 at 3:39 • The caller can wrap the called function in Catch to stop propagation to the top level and handle it as needed. Have you tried that? – Rohit Namjoshi Feb 29 at 4:34 Here's a simple way to do that. First define something to validate your output and which will Throw a tag if invalid. Then Catch that: validateResp // Clear validateResp[resp_, validator_, error_: Automatic] := If[! TrueQ[validator@resp], Throw[resp, Replace[error, Automatic :> $$tryCatchTag]], resp ]; tryCatch // Clear SetAttributes[tryCatch, HoldFirst]; tryCatch[ expr_, error : _String | Verbatim[Blank][] : "BadValue", handler : Except[_String | Verbatim[Blank][]] : (#2 &), validator : Except[_String | Verbatim[Blank][]] : (Length[$$MessageList] == 0 &) ] := Block[{$tryCatchTag = error},
Catch[
validateResp[expr, validator, error],
error,
handler
]
]


You'd use it like this:

tryCatch[
1/0
]

(* Message: Power::infy: Infinite expression 1/0 encountered. *)



Unfortunately there's no global sense of "if an error occurred" so by default my validator just checks if any messages were generated when evaluating, but this can be anything. You can also put a deeper validateResp in there if you want. You can also easily throw your own errors and catch anything like:

tryCatch[
If[! TrueQ[n > 2],
1/0
],
_
]



Which if the first test passed would give you

tryCatch[
If[False,