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

Throw::Back - Throw back exceptions like a boss.

VERSION

This document describes Throw::Back version 0.01

SYNOPSIS

use Try::Tiny;
use Throw::Back;
…
try {
    …
    throw::back("bork bork bork");
    …
    File::Slurp->throw::back("cookie cookie cookie", …);
    …
    $foo->throw::text("You failed [quant,_1,time,times].", [42], …);
    …
}
catch {
    my $err = $_;
    if ( $err->{type} eq 'File::Slurp' ){
        die $err->{string}; # not the place to deal w/ FS issues
    }
    elsif ($err->{type} eq ref($foo)) {
        $ak->out->note("FYI Foo complained a bit: $err"); # Foo barfs a lot for no fatal reason, just FYI and let it ride
    }
    else {
        $ak->logger->debug("Unhandled exception: " . $ak->str->dump($err));
    }
};

DESCRIPTION

We should be able to throw exceptions without using a keyword, UNIVERSAL::, or other heavy shenanigans.

Depending on the circumstance, we may need to use it as a function, class method, or object method.

Also we absolutely must support localized errors.

Oh and also do all of that with no deps, lets keep it light!

INTERFACE

In void context: the Throw::Back exception object is thrown right then and there.

In non-void context: the Throw::Back exception object is returned for the caller to handle as they wish.

The Throw::Back exception object stringifies.

The Throw::Back exception object always has these keys:

string

The exception text.

type

Name space of the exception’s type.

file

The file where the throw::back occurred.

line

The line number where the throw::back occurred.

previous_exception

Exists and is set to $@ if $@ contains anything at the time of throwing.

If a class or object is used then “exception” may also be set as documented below under #2 and #3.

3 ways to throw::back()

1. via function
throw::back;
throw::back("Your string here")
throw::back({…})
throw::back(…)
2. via class method
Any::Class::Here->throw::back(…);

If 'Any::Class::Here' is loaded or loadable and has a new() method then the exception object’s “exception” key is set to a new object (the arguments to throw::back() get passed to new()).

If it can’t be loaded or does not have a new() method then the type and string keys will reflect that.

3. via object method

The exception object’s “exception” key is set to the object.

throw::stack

Like throw::back but also includes the call stack in a key called “call_stack”.

When the object is stringified, the stack array gets formatted and included.

throw::text

Like throw::back but the string given is treated as a maketext format phrase.

If your phrase requires arguments they can be passed in via array ref as the final argument.

If called as a function or the class/object can not process them then the keys “phrase” and “phrase_args” contain the maketext data.

If called w/ class/object that can make it (either has the methods needed or has a façade method called “locale” that has the methods) then “string” is the maketext()d version. “string_not_localized” will also exist if the object has a makethis_base method.

throw::stack::text

Like throw::stack() but with the behavior of throw::text().

DIAGNOSTICS

Throw::Back throws no warnings or errors of its own.

CONFIGURATION AND ENVIRONMENT

Throw::Back requires no configuration files or environment variables.

DEPENDENCIES

None

INCOMPATIBILITIES

None reported.

Why Throw::Back?

It made more sense than Throw::Down and was less disturbing than Throw::Up.

CAVEAT

A one word message is pointless so don’t do that!

It is also ambiguous since there is no way to differentiate between a one word message and a name space thus you’ll get unexpected results, so don’t do that!

TODO

- clarify interface/POD

- Stack polish (args, pkg, etc).

- More thorough tests.

- Add rethrow()/PROPAGATE() support.

BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to bug-throw-back@rt.cpan.org, or through the web interface at http://rt.cpan.org.

AUTHOR

Daniel Muey <http://drmuey.com/cpan_contact.pl>

LICENCE AND COPYRIGHT

Copyright (c) 2013, Daniel Muey <http://drmuey.com/cpan_contact.pl>. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.