September 2017
Beginner
402 pages
9h 52m
English
Now we can start examining exception objects. The Exception base class defines the throw method, which you can use to throw an exception. For simplicity, let us start with the X::AdHoc exception.
In the following program, an exception is explicitly created with the new method and immediately thrown with the throw method:
say 'Start';
X::AdHoc.new.throw;
say 'Stop';
CATCH {
say 'Caught';
}
The output of the program is familiar to us:
Start Caught Unexplained error in block <unit> at throw.pl line 2
We did not provide any error message, and the program printed the default string—Unexplained error.
To use a message, use the payload named argument when creating an exception object:
X::AdHoc.new(payload => 'My error message').throw; ...
Read now
Unlock full access