Exception::Tiny(3pm)

πŸ“¦
version 0.2.1 Β· 3 versions
, in fedora 43

πŸ’– 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