Unexpected::TraitFor::ExceptionClasses - Define an exception class hierarchy
package YourExceptionClass; use Moo; extends 'Unexpected'; with 'Unexpected::ExceptionClasses'; __PACKAGE__->add_exception( 'A' ); __PACKAGE__->add_exception( 'B', { parents => 'A' } ); __PACKAGE__->add_exception( 'C', 'A' ); # same but shorter __PACKAGE__->add_exception( 'D', [ 'B', 'C' ] ); # diamond pattern # Then elsewhere __PACKAGE__->throw( 'error message', { class => 'C' } ); # Elsewhere still my $e = __PACKAGE__->caught; $e->class eq 'C'; # true $e->instance_of( 'A' ); # true $e->instance_of( 'B' ); # false $e->instance_of( 'C' ); # true $e->instance_of( 'D' ); # false
Allows for the creation of an exception class hierarchy. Exception classes inherit from one or more existing classes, ultimately all classes inherit from the Unexpected exception class
Unexpected
Defines the following attributes;
class
Defaults to Unexpected. Can be used to differentiate different classes of error. Non default values for this attribute must have been defined with a call to "add_exception" otherwise an exception will be thrown. Oh the irony
Defines the Unspecified exception class with a default error message. Once the exception class is defined the the following;
Unspecified
use Unexpected::Functions qw( Unspecified );
will import a subroutine that when called returns it's own name. This is a suitable value for the class attribute
YourExceptionClass->throw( class => Unspecified, args => [ 'param name' ] );
Applies the default error message if one exists and the attributes for the soon to be constructed exception lacks one
YourExceptionClass->add_exception( 'new_classname', [ 'parent1', 'parent2']);
Defines a new exception class. Parent classes must already exist. Default parent class is Unexpected;
$class->add_exception( 'new_classname' => { parents => [ 'parent1' ], error => 'Default error message [_1]' } );
Sets the default error message for the exception class
When defining your own exception class import and call has_exception which will call this class method. The functions subroutine signature is like that of has in Moo
has
Moo
$bool = $exception_obj->instance_of( 'exception_classname' );
Is the exception object an instance of the exception class
$bool = YourExceptionClass->is_exception( 'exception_classname' );
Returns true if the exception class exits as a result of a call to "add_exception"
None
There are no known incompatibilities in this module
There are no known bugs in this module. Please report problems to http://rt.cpan.org/NoAuth/Bugs.html?Dist=Unexpected. Patches are welcome
Larry Wall - For the Perl programming language
Peter Flanigan, <pjfl@cpan.org>
<pjfl@cpan.org>
Copyright (c) 2015 Peter Flanigan. All rights reserved
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic
This program is distributed in the hope that it will be useful, but WITHOUT WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
To install Unexpected, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Unexpected
CPAN shell
perl -MCPAN -e shell install Unexpected
For more information on module installation, please visit the detailed CPAN module installation guide.