Error-Subclasses

 view release on metacpan or  search on metacpan

lib/Error/SystemException.pm  view on Meta::CPAN

    my $class = shift;
    my $perror = "$!";
    my $errno = $!+0;

    my ( $message ) = @_;

    local $Error::Depth = $Error::Depth + 1;

    my $self = $class->SUPER::new( -text => $message, -value => $errno );

    $self->{perror} = $perror;

    $self;
}

=head2 $str = $self->perror

This function returns the stored string value of Perl's C<$!> variable at the
time the exception object was created.

=cut

sub perror
{
    my $self = shift;
    return $self->{perror};
}

sub stringify
{
    my $self = shift;
    return $self->SUPER::stringify() . " - " . $self->perror;
}

# Keep perl happy; keep Britain tidy
1;

__END__

=head1 EXAMPLES

Typically, this exception class would be used following the failure of a
system call.

 mkdir( $dir ) or throw Error::SystemException( "Cannot mkdir( '$dir' )" );

If caught, this exception would print a message perhaps looking like

 Cannot mkdir( '/root/testdir' ) - Permission denied

Because it is a subclass of C<Error>, the usual try/catch mechanisms also
apply to it.

 try {
     mkdir( $dir ) 
         or throw Error::SystemException( "mkdir($dir)" );

     try {
         chmod( $mode, $dir )
             or throw Error::SystemException( "chmod($dir)" );
         chown( $uid, $gid, $dir )
             or throw Error::SystemException( "chown($dir)" );
     }
     catch Error with {
         my $e = shift;
         rmdir( $dir );
         $e->throw;
     };
 }
 catch Error with {
     my $e = shift;
     # handle $e here...
 };

=head1 SEE ALSO

=over 4

=item *

L<Error> - Base module for exception-based error handling

=back

=head1 AUTHOR

Paul Evans <leonerd@leonerd.org.uk>



( run in 1.402 second using v1.01-cache-2.11-cpan-71847e10f99 )