View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Peter Flanigan > Class-Usul > Class::Usul::Exception



Annotate this POD



Open  0
View/Report Bugs
Module Version: v0.84.1   Source  

Name ^

Class::Usul::Exception - Exception handling

Synopsis ^

   use Class::Usul::Functions qw(throw);
   use Try::Tiny;

   sub some_method {
      my $self = shift;

      try   { this_will_fail }
      catch { throw $_ };

   # OR
   use Class::Usul::Exception;

   sub some_method {
      my $self = shift;

      eval { this_will_fail };

   # THEN
   try   { $self->some_method() }
   catch { warn $_."\n\n".$_->stacktrace."\n" };

Description ^

An exception class that supports error messages with placeholders, a "throw" method with automatic re-throw upon detection of self, conditional throw if an exception was caught and a simplified stacktrace

Error objects are overloaded to stringify to the full error message plus a leader

Configuration and Environment ^

The __PACKAGE__->ignore_class class method contains a classes whose presence should be ignored by the error message leader

Defines the following list of read only attributes;


An array ref of parameters substituted in for the placeholders in the error message when the error is localised


Defaults to __PACKAGE__. Can be used to differentiate different classes of error


The actually error message which defaults to Unknown error. Can contain placeholders of the form [_<n>] where <n> is an integer starting at one


Set to the package and line number where the error should be reported


A positive integer which defaults to one. How many additional stack frames to pop before calculating the leader attribute


Defaults to null. May contain the output from whatever just threw the exception


Return value which defaults to one


A positive integer which defaults to the CORE::time the exception was thrown

Subroutines/Methods ^


Doesn't modify the BUILDARGS method. This is here to workaround a bug in Moo and / or Test::Pod::Coverage


   $error_text = $self->as_string;

This is what the object stringifies to, including the leader attribute


   $self = $class->caught( [ @args ] );

Catches and returns a thrown exception or generates a new exception if $EVAL_ERROR has been set. Returns either an exception object or undef


   $cloned_exception_object_ref = $self->clone( $args );

Returns a clone of the invocant. The optional $args hash reference mutates the returned clone


   $lines = $self->stacktrace( $num_lines_to_skip );

Return the stack trace. Defaults to skipping zero lines of output


   $class->throw 'Path [_1] not found', [ 'pathname' ];

Create (or re-throw) an exception. If the passed parameter is a blessed reference it is re-thrown. If a single scalar is passed it is taken to be an error message, a new exception is created with all other parameters taking their default values. If more than one parameter is passed the it is treated as a list and used to instantiate the new exception. The 'error' parameter must be provided in this case


   $class->throw_on_error( [ @args ] );

Calls "caught" passing in the options @args and if there was an exception "throw"s it

Diagnostics ^


Dependencies ^


Incompatibilities ^

There are no known incompatibilities in this module

Bugs and Limitations ^

There are no known bugs in this module. Please report problems to the address below. Patches are welcome

Author ^

Peter Flanigan <>

License and Copyright ^

Copyright (c) 2018 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

syntax highlighting: