The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Stuff::Exception - Exception with context

SYNOPSIS

  # Create own exception class.
  package HttpAbortException;
  use Stuff -Exception; # <= push @ISA, qw( Stuff::Exception );
  has status => 0;
  has no_stack_trace => 1;
  
  # Instantiate exception.
  my $e = HttpAbortException->new( status => 404, message => 'Request aborted' );
  
  # Get attibute.
  say $e->status; # => 404
  
  # Automatic stringification.
  say $e; # => 'Request aborted'
  
  # Throw exception.
  $e->throw;
  
  # Or instantiate and throw.
  HttpAbortException->throw( status => 404 );
  
  eval {
    HttpAbortException->throw( status => 404 );
  };
  
  $@->status; # => 404

METHODS

Stuff::Exception inherit all methods from Stuff::StackTrace and implements the following:

new

  $package->new;
  $package->new( $message );
  $package->new( %args );

throw

  $package->throw;
  $package->throw( $message );
  $package->throw( %args );
  $object->throw;

to_string

  $object->to_string;
  "$object";

Returns a string that represents exception. By default it is a message translated to string.

ATTRIBUTES

message

Optional message associated with exception. In general cases it can be any object, not string or number only.

no_frames

If it has true value then no stack frames will be collected duering object instantiation. Default value is false.

SEE ALSO

Stuff, Stuff::Exception

AUTHOR

Nikita Zubkov <nikzubkov@gmail.com>.