Skip to content

Instantly share code, notes, and snippets.

@codesections
Created March 8, 2021 21:13
Show Gist options
  • Save codesections/df7ac67ba6e88ef907dd3095dc9b8641 to your computer and use it in GitHub Desktop.
Save codesections/df7ac67ba6e88ef907dd3095dc9b8641 to your computer and use it in GitHub Desktop.
Comparing `try` to a CATCH block in Raku
my $result = do try {
# some code that may throw X::This
# some code that may throw X::NotSpecified (default)
# some code that may throw X::Something
# some code that may throw X::This or X::That
# some code that may fail instead of throw
# (sunk so that it will throw immediately)
sink may-fail;
} // do given {
when X::Something { … }
when X::This { … }
when X::That { … }
default { … }
}
#################### Instead of #######################
my $result = do {
CATCH {
when X::Something { … }
when X::This { … }
when X::That { … }
default { … }
}
# some code that may throw X::This
# some code that may throw X::NotSpecified (default)
# some code that may throw X::Something
# some code that may throw X::This or X::That
# some code that may fail instead of throw
# (sunk so that it will throw immediately)
sink may-fail;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment