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

$error_string = $object->message()

Returns the error message (a string).

caller

$caller_aref = $object->caller()

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

$error_info = $object->as_string()

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

raise

$object->raise()
$class->raise($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

$sub_result = $class->trap_into($sub)

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

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