Let us create a program that tries to open a nonexisting file and read the first line from it:
my $f = open 'dummy.txt'; say $f.get;
This program will raise an exception:
Failed to open file /Users/ash/code/exceptions/dummy.txt: no such file or directory in block <unit> at 14.pl line 1
Notice that the exception happens only after an attempt to read from a file happens. Simply opening a file does not create an error, it only sets the $f file handler to the Failure object.
The failure object is a wrapper around an Exception object. The exception itself is reachable via the exception method:
my $f = open 'dummy.txt'; say $f.exception;
It prints the error message:
Failed to open file /Users/ash/code/exceptions/dummy.txt: no such file or directory
You can test a failure object in the Boolean context, for example, immediately after opening...