summaryrefslogtreecommitdiffhomepage
path: root/perl6.html.markdown
diff options
context:
space:
mode:
authorNami-Doc <vendethiel@hotmail.fr>2014-09-23 23:28:24 +0200
committerNami-Doc <vendethiel@hotmail.fr>2014-09-23 23:28:24 +0200
commitb6ba516743d4362b3f079872b1e60c1a54e72aee (patch)
tree65a833dd942447bfdad412e165dd0c785ac09241 /perl6.html.markdown
parent40620d68164527bef4c7329c303598a4722b476f (diff)
Exception thingies!
Diffstat (limited to 'perl6.html.markdown')
-rw-r--r--perl6.html.markdown33
1 files changed, 29 insertions, 4 deletions
diff --git a/perl6.html.markdown b/perl6.html.markdown
index 4e7d8c6e..d12b99ae 100644
--- a/perl6.html.markdown
+++ b/perl6.html.markdown
@@ -700,15 +700,37 @@ try {
open 'foo';
CATCH {
when X::AdHoc { say "unable to open file !" }
- # any other exception will be re-raised, since we don't have a `default`
+ # Any other exception will be re-raised, since we don't have a `default`
+ # Basically, if a `when` matches (or there's a `default`) marks the exception as
+ # "handled" so that it doesn't get re-thrown from the `CATCH`.
+ # You still can re-throw the exception (see below) by hand.
}
}
# You can throw an exception using `die`:
die X::AdHoc.new(payload => 'Error !');
-# TODO warn
-# TODO fail
-# TODO CONTROL
+
+# You can access the last exception with `$!` (usually used in a `CATCH` block)
+
+# There are also some subtelties to exceptions. Some Perl 6 subs return a `Failure`,
+# which is a kind of "unthrown exception". They're not thrown until you tried to look
+# at their content, unless you call `.Bool`/`.defined` on them - then they're handled.
+# (the `.handled` method is `rw`, so you can mark it as `False` back yourself)
+#
+# You can throw a `Failure` using `fail`. Note that if the pragma `use fatal` is on,
+# `fail` will throw an exception (like `die`).
+fail "foo"; # We're not trying to access the value, so no problem.
+try {
+ fail "foo";
+ CATCH {
+ default { say "It threw because we try to get the fail's value!" }
+ }
+}
+
+# There is also another kind of exception: Control exceptions.
+# Those are "good" exceptions, which happen when you change your program's flow,
+# using operators like `return`, `next` or `last`.
+# You can "catch" those with `CONTROL` (not 100% working in Rakudo yet).
### Packages
# Packages are a way to reuse code. Packages are like "namespaces", and any
@@ -1351,6 +1373,9 @@ so 'ayc' ~~ / a [ b | y ] c /; # `True`. Obviously enough ...
# Note: the first-matching `or` still exists, but is now spelled `||`
'foo' ~~ / fo || foo /; # `fo` now.
+
+
+
### Extra: the MAIN subroutime
# The `MAIN` subroutine is called when you run a Perl 6 file directly.
# It's very powerful, because Perl 6 actually parses the argument