Using TryCatch you’d write:
try { ... }
catch (SomeClass $e) { use($e) }
catch (SomethingElse $e) { use($e) }
catch ($e) { use($e) }
Using Try::Tiny you’d write:
try { ... }
catch {
# here you get the exception in $_
when (match_instance_of('SomeClass')) { use($_) }
when (match_instance_of('SomethingElse')) { use($_) }
default { use($_) }
}; # note the semi-colon
On the other hand, if your TryCatch use did not have a unqualified "catch ($e)", you need to write "default { die $_ }" to re-throw the unhandled exception (yes, you really have to write "die
$_", read the documentation of die to learn the ugly details; "die" without arguments won’t do anything useful there).
Also, keep in mind that the blocks used by Try::Tiny are actually anonymous subroutines, so they get their own @_ (nothing in the case of the "try" block, the exception in the case of the
"catch" block), and "return" will return from the block, not the containing subroutine.
Devel::Declare (via TryCatch) is deep scary voodoo.
From #backend:
- Try::Tiny is the less magical and scary (and fragile) version of TryCatch
- Syntax::Feature::Try is currently the least offensive of the alternatives, but it has quite a way to go
- it does nasty things with the call stack
- preferrably it would splice the optree, like a compiler macro
- How does return work? TryCatch returns from the surrounding sub, Try::Tiny returns form the try {} block