Question: Why does this fail?
In:= (Catch[#] &)[Throw[17]]
Throw::nocatch : Uncaught Throw[17] returned to top level. >> Out= Hold[Throw[17]]
These other functional forms also fail:
Function[{x}, Catch[x]][Throw[17]]jCatch[x_] := Catch[x]; jCatch[Throw[17]]
Background
I recently wasted time tracking down an error from Return[]'s nonintuitve behavior (It doesn't breaks from For loops but does from Do loops!?). So I tried to replace the Return in a function with Throw and Catch. I had been advised that it's never good idea to use these without tags, but I don't like having the tag for Catch all the way at the bottom of the function because this makes it harder, when looking at a given Throw, to locate the corresponding Catch.
f[x_]:=Catch[
...
...
If[y<z,Throw[a,"tag"];
...
...
Throw[b,"tag"];
,"tag"];
Maybe I'm unreasonable, but I'd rather have the tag at the beginning. So I tried
f[x_]:=(Catch[#,"tag"]&)[
...
...
If[y<z,Throw[a,"tag"];
...
...
Throw[b,"tag"];
];
But this fails as explained above.
Is there a way to get the tag at the front of the code rather than end? Is there another construct I should be using instead? I hear people bad-mouth nonlocal code jumps, but what's the alternative here, putting the entire rest of the function in the 'else' parameter of the If statement?
Function[{x}, Catch[x], HoldAll][Throw[17]]works, tho. – J. M.'s missing motivation May 29 '16 at 13:50jCatch = Function[{tg,x}, Catch[x,tg], HoldAll];and define a function asf[x_]:= jCatch["tag", ... Throw[a, "tag"]];. Can you explain what's going on here? I see that it must be closely related to the fact thatCatch[Evaluate[Throw[17]]]gives an error, but I don't see why. What does it mean that theThrowis "returned to the top level" when it's obviously surrounded by theCatch? – Jess Riedel May 29 '16 at 14:00Throw[]isn't supposed to evaluate unless there's an enclosingCatch[].Catch[Evaluate[Throw[17]]]has theThrow[]evaluate before being seen byCatch[], leading to your observed behavior. Another one for your notes:Function[Null, Catch[##], HoldAll][Throw[17]]. – J. M.'s missing motivation May 29 '16 at 14:07jCatchto accept both (a) one argument (the tag) and return a single argument functionFunction[{exp},Catch[exp,tag],HoldAll], or alternatively (b) two arguments (the tag and the expression) and returnCatch[exp,tag]. I need to define it withFunctionrather than&or a replacement rule, since I need to useHoldAll. And (it seems) I need to use named arguments (Function[{tag,exp},...]) because I want to return a function (i.e., I'll have nested function ). – Jess Riedel May 29 '16 at 15:03jCatch[x]to treatxas a tag and return the functionFunction[{exp},Catch[exp,x],HoldAll]. If I definejCatch=Function[Null, Catch[##], HoldAll]and give it a single variable, this variable is treated as the expression byCatch, because that's whatCatchdoes with single variables. The behavior I'm looking for is similar toSortBy, which returns a function if given a single argument. – Jess Riedel May 29 '16 at 15:17