Skip to content

Commit 93d0fd1

Browse files
committed
Documents Exception.fail closes #3383
1 parent 75f1dc5 commit 93d0fd1

File tree

1 file changed

+19
-32
lines changed

1 file changed

+19
-32
lines changed

doc/Type/Exception.pod6

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -94,43 +94,30 @@ backtrace.
9494
9595
Defined as:
9696
97-
multi sub fail(*@text)
9897
multi sub fail(Exception $e)
99-
method fail(Exception:D:)
98+
method fail(Exception:D:)
10099
101100
Exits the calling C<Routine> and returns a L<Failure|/type/Failure> object wrapping the
102-
exception C<$e> - or, for the C<*@text> form, an L<X::AdHoc|/type/X::AdHoc> exception
103-
constructed from the concatenation of C<@text>. If the caller
104-
activated fatal exceptions via the pragma C<use fatal;>, the exception is
105-
thrown instead of being returned as a C<Failure>.
101+
exception.
106102
103+
=begin code
104+
# A custom exception defined
105+
class ForbiddenWord is Exception {
106+
has Str $.word;
107+
method message { "This word is forbidden: «$!word»" }
108+
}
109+
110+
sub say-word ( $word ) {
111+
ForbiddenWord.new(:word($word)).fail if $word eq 'foo';
112+
$word.say;
113+
}
114+
115+
my $result = say-word("foo");
116+
say $result.exception;
117+
=end code
107118
108-
# A custom exception defined
109-
class ForbiddenDirectory is Exception {
110-
has Str $.name;
111-
112-
method message { "This directory is forbidden: '$!name'" }
113-
}
114-
115-
sub copy-directory-tree ($dir) {
116-
# We don't allow for non-directories to be copied
117-
fail "$dir is not a directory" if !$dir.IO.d;
118-
# We don't allow 'foo' directory to be copied too
119-
fail ForbiddenDirectory.new(:name($dir)) if $dir eq 'foo';
120-
# or above can be written in method form as:
121-
# ForbiddenDirectory.new(:name($dir)).fail if $dir eq 'foo';
122-
# Do some actual copying here
123-
...
124-
}
125-
126-
# A Failure with X::AdHoc exception object is returned and
127-
# assigned, so no throwing Would be thrown without an assignment
128-
my $result = copy-directory-tree("cat.jpg");
129-
say $result.exception; # OUTPUT: «cat.jpg is not a directory␤»
130-
131-
# A Failure with a custom Exception object is returned
132-
$result = copy-directory-tree('foo');
133-
say $result.exception; # OUTPUT: «This directory is forbidden: 'foo'␤»
119+
The routine form works in the same way, with an alternative syntax:
120+
C<fail ForbiddenWord.new(:word($word))>.
134121
135122
=head2 method gist
136123

0 commit comments

Comments
 (0)