The Exception object
Exceptions in Perl 6 are handled via the objects of the classes that are derived from the Exception
class. These objects contain all the necessary information regarding the exception, including some text description and stack trace (in Perl 6, it is called backtrace).
Perl 6 creates an exception object when an exception arises. We have seen an example of such a situation earlier in this chapter—the error became visible during an attempt to print the result of the illegal mathematical operation. Now, let us produce an exception ourselves using the die
keyword.
The die
keyword throws a fatal exception and terminates the program. A typical usage is to stop the program if it cannot open a file or load a resource that is vital for the rest of the program, for example:
my $fh = open 'filename.txt' or die 'File not found';
If there is no such file, the $fh
variable in the Boolean context is false and the second branch of the or
operator will be executed.
The die
function accepts...