| Exception-System documentation | Contained in the Exception-System distribution. |
Exception::System - The exception class for system or library calls
# The simplest usage
use Exception::Base 'Exception::System';
open my $file, "/notfound"
or Exception::System->throw(message=>"Can not open file");
# The Exception::System class can be a base class for others
#
# Loaded automatically if used as Exception::Base's argument
use Exception::Base,
'Exception::System',
'Exception::File' => {
isa => 'Exception::System',
has => 'filename',
string_attributes => [ 'message', 'errstr', 'filename' ],
};
eval {
my $filename = "/notfound";
open my $fh, $filename
or Exception::File->throw(
message=>"Can not open file",
filename=>$filename,
);
};
if ($@) {
my $e = Exception::Base->catch;
if ($e->isa('Exception::File')) { warn "File error:".$e->errstr; }
if ($e->matches({errname=>'ENOENT'})) { warn "Caught not found error"; }
}
This class extends standard Exception::Base with handling system or library errors. The additional attributes of the exception object are filled on throw and contain the error message and error codes.
Declaration of class attributes as reference to hash.
See Exception::Base for details.
Class attributes are implemented as values of blessed hash. The attributes of base class are inherited. See Exception::Base to see theirs description.
Contains the message of the exception. This class overrides the default value from Exception::Base class.
Contains the system error string fetched at exception throw. It is the part
of the string representing the exception object. It is the same as $!
variable in string context.
eval { Exception::System->throw( message=>"Message" ) };
my $e = Exception::Base->catch
and print $e->errstr;
Contains the extended system error string fetched at exception throw. It is
the same as $^E variable.
eval { Exception::System->throw( message=>"Message" ); };
if ($@) {
my $e = Exception::Base->catch;
if ($e->errstros ne $e->errstr) {
print $e->errstros;
}
}
Contains the system error number fetched at exception throw. It is the same
as $! variable in numeric context. This attribute represents numeric value
of the exception object in numeric context.
use Errno ();
eval { Exception::System->throw( message=>"Message" ); };
if ($@) {
my $e = Exception::Base->catch;
if ($e->errno == &Errno::ENOENT) { # explicity
warn "Not found";
}
elsif ($e == &Errno::EPERM) { # numeric context
warn "Bad permissions";
}
}
Contains the system error constant from the system error.h include file.
eval { Exception::System->throw( message=>"Message" ); };
my $e = Exception::Base->catch
and $e->errname eq 'ENOENT'
and $e->throw;
Meta-attribute contains the format of string representation of exception object. This class overrides the default value from Exception::Base class.
Meta-attribute contains the name of the attribute which contains numeric value of exception object. This class overrides the default value from Exception::Base class.
Collect system data and fill the attributes of exception object. This method is called automatically if exception if throwed. This class overrides the method from Exception::Base class.
If you find the bug, please report it.
Piotr Roszatycki <dexter@debian.org>
Copyright (C) 2007, 2008 by Piotr Roszatycki <dexter@debian.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Exception-System documentation | Contained in the Exception-System distribution. |
#!/usr/bin/perl -c package Exception::System; use 5.006; our $VERSION = 0.11;
use strict; use warnings; # Extend Exception::Base class use Exception::Base 0.20 'Exception::System' => { has => { ro => [ 'errstr', 'errstros', 'errno', 'errname' ] }, message => 'Unknown system exception', verbosity => 3, string_attributes => [ 'message', 'errstr' ], numeric_attribute => 'errno', }; # Use ERRNO hash use Errno (); # Map for errno -> errname (choose the shortest errname string for the same errno number) my %Errname = map { Errno->$_ => $_ } sort { length $b <=> length $a } sort keys (%!); # Collect system data sub _collect_system_data { my $self = shift; $self->{errstr} = "$!"; # string context $self->{errstros} = $^E; $self->{errno} = 0+$!; # numeric context $self->{errname} = $Errname{ $self->{errno} } || ''; return $self->SUPER::_collect_system_data(@_); }; 1; __END__