NAME

Bitcoin::Crypto::Exception - Exception class for Bitcoin::Crypto purposes

SYNOPSIS

        use Try::Tiny;

        try {
                decode_segwit("Not a segwit address");
        } catch {
                my $error = $_;

                # $error is an instance of Bitcoin::Crypto::Exception and stringifies automatically
                warn "$error";

                # but also contains some information about the problem to avoid regex matching
                if ($error->isa("Bitcoin::Crypto::Exception::Bech32InputFormat")) {
                        log $error->message;
                }
        };

DESCRIPTION

A wrapper class with automatic stringification and standarized raising. Contains many other inline packages that identify parts that went wrong (like Bitcoin::Crypto::Exception::Sign for errors in signature generation). See individual Bitcoin::Crypto packages documentation to see the exception classes to check for extra control flow when needed.

FUNCTIONS

message

        sig: message($self)

Returns the error message (a string).

caller

        sig: caller($self)

Returns an array ref containing: package name, file name and line number (same as [caller()] perl expression). It will contain the data for the first code from outside Bitcoin::Crypto which called it. May be undefined if it cannot find a calling source.

as_string

        sig: as_string($self)

Stringifies the error, using the message, caller and some extra text for context.

raise

        sig: raise($self)
             raise($class, $message)

Creates a new instance and throws it. If used on an object, throws it right away.

        use Try::Tiny;

        try {
                # throws, but will be catched
                Bitcoin::Crypto::Exception->raise("something went wrong");
        } catch {
                my $exception = $_;

                # throws again
                $exception->raise;
        };

throw

An alias to raise.

trap_into

        sig: trap_into($class, $sub)

Executes the subroutine given as the only parameter inside an eval. Any exceptions thrown inside the subroutine will be re-thrown after turning them into objects of the given class. If no exception is thrown, method returns the value returned by the subroutine.

        my $result = Bitcoin::Crypto::Exception->trap_into(sub {
                die "something went wrong";
        });