Skip to content

Throw ​

Aborts evaluation up to the nearest enclosing Catch, carrying a value.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageThrow✓
Throw(value)Unwinds evaluation up to the nearest enclosing Catch, which returns valuevalue.
Throw(value, tag)Like Throw(value)Throw(value), but only a Catch(expr,tag)Catch(expr, tag) matching tagtag exactly stops it — an untagged or non-matching CatchCatch lets it propagate further out.
Typed signatures
TypePackageApplies
(any, any?) -> anycombinatoricsalways

Domain: Collections

Details
  • Only exact tag equality is matched, not Wolfram's fuller pattern-matching form.
  • A ThrowThrow that reaches no matching CatchCatch propagates out of the whole evaluation.

Examples

Basic #

Throw aborts evaluation, Catch recovers the value

live ↗
InCatch(Throw(abandoned))\mathrm{Catch}(\mathrm{Throw}(\mathrm{abandoned}))Outabandoned\mathrm{abandoned}
live ↗
InCatch(Do(If(k!>1010,Throw(k)),[k,100]))\mathrm{Catch}(\mathrm{Do}(\mathrm{If}(k!\gt10^{10}, \mathrm{Throw}(k)), \bigl\lbrack k, 100\bigr\rbrack))Out1414
Scope #

a tag mismatch propagates past a non-matching Catch to one that matches

live ↗
InCatch(Catch(Throw(deep,x),y),x)\mathrm{Catch}(\mathrm{Catch}(\mathrm{Throw}(\mathrm{deep}, x), y), x)Outdeep\mathrm{deep}

The thrown value escapes the list and is returned directly

live ↗
InCatch([a,Throw(b),c])\mathrm{Catch}(\bigl\lbrack a, \mathrm{Throw}(b), c\bigr\rbrack)Outbb

Evaluation stops at the first Throw, so only aa comes back

live ↗
InCatch([Throw(a),Throw(b),Throw(c)])\mathrm{Catch}(\bigl\lbrack\mathrm{Throw}(a), \mathrm{Throw}(b), \mathrm{Throw}(c)\bigr\rbrack)Outaa

A tagged throw is caught by a Catch with the matching tag

live ↗
InCatch(Throw(a,u),u)\mathrm{Catch}(\mathrm{Throw}(a, u), u)Outaa

An inner Catch absorbs the throw, and the outer evaluation continues

live ↗
InCatch([Catch([a,Throw(b),c]),d,k])\mathrm{Catch}(\bigl\lbrack\mathrm{Catch}(\bigl\lbrack a, \mathrm{Throw}(b), c\bigr\rbrack), d, k\bigr\rbrack)Out[b,d,k]\bigl\lbrack b, d, k\bigr\rbrack
Applications #
live ↗
InCatch(Do(If(IsPrime(k),Throw(k)),[k,1010,1010+1 000]))\mathrm{Catch}(\mathrm{Do}(\mathrm{If}(\mathrm{IsPrime}(k), \mathrm{Throw}(k)), \bigl\lbrack k, 10^{10}, 10^{10}+1\,000\bigr\rbrack))Out10 000 000 01910\,000\,000\,019
live ↗
InCatch(Do(If(17k mod 19=1,Throw(k)),[k,19]))\mathrm{Catch}(\mathrm{Do}(\mathrm{If}(17^{k}\bmod19=1, \mathrm{Throw}(k)), \bigl\lbrack k, 19\bigr\rbrack))Out99
live ↗
InCatch(NestList(_1⁡↦if _1⁡>1 000 then Throw(_1⁡) else _1⁡2+1,2,6))\mathrm{Catch}(\mathrm{NestList}(\operatorname{\_1}↦\text{if }\operatorname{\_1}\gt1\,000\text{ then }\mathrm{Throw}(\operatorname{\_1})\text{ else }\operatorname{\_1}^2+1, 2, 6))Out458 330458\,330
Possible issues #
live ↗
InCatch(NestList(_1⁡↦if _1⁡>1 000 then Throw(_1⁡) else _1⁡2+1,1,7))\mathrm{Catch}(\mathrm{NestList}(\operatorname{\_1}↦\text{if }\operatorname{\_1}\gt1\,000\text{ then }\mathrm{Throw}(\operatorname{\_1})\text{ else }\operatorname{\_1}^2+1, 1, 7))Out458 330458\,330
About
Examples11
Checked againstWolfram Language 11 agree
CrosswalkWolfram Language

See also: Catch