Exception::Tiny(3pm)
π NAME π
Exception::Tiny β it's giving "too tiny exception interface", bestie. That's literally the whole name. No notes. π
β¨ SYNOPSIS β¨
okay so first up, a simple example. look at her go:
package MyException;
use parent 'Exception::Tiny';
package main;
# try
sub foo {
eval {
MyException->throw( 'oops!' ); # same MyException->throw( message => 'oops!' );
};
}
# catch
if (my $e = $@) {
if (MyException->caught($e)) {
say $e->message; # show 'oops!'
say $e->package; # show 'main'
say $e->file; # show 'foo.pl'
say $e->line; # show '9'
say $e->subroutine; # show 'main:foo'
say $e->dump; # dump self
say $e; # show 'oops! at foo.pl line 9.'
$e->rethrow; # rethrow MyException exception.
}
}
next: can you accessor for exception class? yes bestie, absolutely you can:
package MyExceptionBase;
use parent 'Exception::Tiny';
use Class::Accessor::Lite (
ro => [qw/ status_code /],
);
package MyException::Validator;
use parent -norequire, 'MyExceptionBase';
use Class::Accessor::Lite (
ro => [qw/ dfv /],
);
package main;
# try
eval {
MyException::Validator->throw(
message => 'oops',
status_code => '500',
dfv => {
missing => 'name field is missing.',
},
);
};
# catch
if (my $e = $@) {
if (MyException->caught($e)) {
say $e->message; # show 'oops';
say $e->status_code; # show '500';
say $e->dfv->{missing}; # show 'name field is missing.'
say $e; # show 'oops at bar.pl line 17.'
}
}
and third one, because we love a trilogy: can you catche nested class? slay, yes:
package BaseException;
use parent 'Exception::Tiny';
package MyException::Validator;
use parent -norequire, 'BaseException';
package main;
eval { MyException::Validator->throw }
my $e = $@;
say $e if BaseException->caught($e); # show 'MyException::Validator at bar.pl line 9.'
πΈ DESCRIPTION πΈ
Exception::Tiny is, and I cannot stress this enough, a too simple exception interface. π§ It's the implementation of the minimum required in order to implement exception handling. Like, the bare minimum. The "I just need it to work" version. And that's the whole point, bestie β so literally anyone can understand the implementation of It. no gatekeeping, no 4000 lines of mystery code, just vibes and clarity. π
π¦ CLASS METHODS π¦
π throw( ... )
throw the exception. that's it, that's the job. just throw her. π«Ά
π caught($e)
okay so here's the vibe check: it returns an exception object if the argument is of the current class, or a subclass of that class. and if it's not? it simply returns $e. no drama, no exception about the exception. π
π INSTANCE METHODS π
π rethrow
re-throw the exception object. like, we're throwing her again. she's back. π₯
π message
It return the exception message. the default is the exception class name β so if you don't write a message, you still get something, iconic. π
π package
It return the package name that exception has occurred. where did it happen? right here, bestie. π
π file
It return the file name that exception has occurred. π
π line
It return the line number in file that exception has occurred. the receipts. π§Ύ
π subroutine
It return the subroutine name that exception has occurred. who was on duty when it happened. π
π as_string
It returned in the format the exception contents of a simple string. and yes β You can Implementation overridden. customize her, make her yours. β¨
π dump
It to dump the contents of the instance. and also β You can Implementation overridden. go off. ποΈβ¨
π HACKING IDEA π
If you want Exception::Class::Base style object, you can write like code of the under. yes, we're hacking. no, we're not sorry. π
package HackException;
use parent 'Exception::Tiny';
use Class::Accessor::Lite (
ro => [qw/ time pid uid euid gid egid /],
);
sub new {
my($class, %args) = @_;
%args = (
%args,
time => CORE::time,
pid => $$,
uid => $<,
euid => $>,
gid => $(,
egid => $),
);
$class->SUPER::new(%args);
}
eval {
HackException->throw;
};
my $e = $@;
say $e->time;
say $e->pid;
say $e->uid;
say $e->euid;
say $e->gid;
say $e->egid;
π AUTHOR π
Kazuhiro Osawa <yappo {@} shibuya {dot} pl>
π SEE ALSO π
- Class::Accessor::Lite
π LICENSE π
Copyright (C) Kazuhiro Osawa
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
okay that's the whole page, bestie, you ate it π
this little manual page is from perl v5.42.0, dated 2025-07-25, and it lives under the name Exception::Tiny(3pm). go forth and throw responsibly. mwah. πβ¨π¦
π¦ where this page comes from
- fedora 43: perl-Exception-Tiny 0.2.1-33.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms