1Exception::Tiny(3)    User Contributed Perl Documentation   Exception::Tiny(3)
2
3
4

NAME

6       Exception::Tiny - too tiny exception interface
7

SYNOPSIS

9       simple example:
10
11         package MyException;
12         use parent 'Exception::Tiny';
13
14         package main;
15
16         # try
17         sub foo {
18             eval {
19                 MyException->throw( 'oops!' ); # same MyException->throw( message => 'oops!' );
20             };
21         }
22
23         # catch
24         if (my $e = $@) {
25             if (MyException->caught($e)) {
26                 say $e->message; # show 'oops!'
27                 say $e->package; # show 'main'
28                 say $e->file; # show 'foo.pl'
29                 say $e->line; # show '9'
30                 say $e->subroutine; # show 'main:foo'
31                 say $e->dump; # dump self
32                 say $e; # show 'oops! at foo.pl line 9.'
33                 $e->rethrow; # rethrow MyException exception.
34             }
35         }
36
37       can you accessor for exception class:
38
39         package MyExceptionBase;
40         use parent 'Exception::Tiny';
41         use Class::Accessor::Lite (
42             ro => [qw/ status_code /],
43         );
44
45         package MyException::Validator;
46         use parent -norequire, 'MyExceptionBase';
47         use Class::Accessor::Lite (
48             ro => [qw/ dfv /],
49         );
50
51         package main;
52
53         # try
54         eval {
55             MyException::Validator->throw(
56                 message     => 'oops',
57                 status_code => '500',
58                 dfv         => {
59                     missing => 'name field is missing.',
60                 },
61             );
62         };
63
64         # catch
65         if (my $e = $@) {
66             if (MyException->caught($e)) {
67                 say $e->message; # show 'oops';
68                 say $e->status_code; # show '500';
69                 say $e->dfv->{missing}; # show 'name field is missing.'
70                 say $e; # show 'oops at bar.pl line 17.'
71             }
72         }
73
74       can you catche nested class:
75
76         package BaseException;
77         use parent 'Exception::Tiny';
78
79         package MyException::Validator;
80         use parent -norequire, 'BaseException';
81
82         package main;
83
84         eval { MyException::Validator->throw }
85
86         my $e = $@;
87         say $e if BaseException->caught($e); # show 'MyException::Validator at bar.pl line 9.'
88

DESCRIPTION

90       Exception::Tiny is too simple exception interface.  This is the
91       implementation of the minimum required in order to implement exception
92       handling.  So anyone can understand the implementation It.
93

CLASS METHODS

95   throw( ... )
96       throw the exception.
97
98   caught($e)
99       It returns an exception object if the argument is of the current class,
100       or a subclass of that class. it simply returns $e.
101

INSTANCE METHODS

103   rethrow
104       re-throw the exception object.
105
106   message
107       It return the exception message.  default is exception class name.
108
109   package
110       It return the package name that exception has occurred.
111
112   file
113       It return the file name that exception has occurred.
114
115   line
116       It return the line number in file that exception has occurred.
117
118   subroutine
119       It return the subroutine name that exception has occurred.
120
121   as_string
122       It returned in the format the exception contents of a simple string.
123       You can Implementation overridden.
124
125   dump
126       It to dump the contents of the instance.  You can Implementation
127       overridden.
128

HACKING IDEA

130       If you want Exception::Class::Base style object, you can write like
131       code of the under.
132
133         package HackException;
134         use parent 'Exception::Tiny';
135         use Class::Accessor::Lite (
136             ro => [qw/ time pid uid euid gid egid /],
137         );
138
139         sub new {
140             my($class, %args) = @_;
141             %args = (
142                 %args,
143                 time => CORE::time,
144                 pid  => $$,
145                 uid  => $<,
146                 euid => $>,
147                 gid  => $(,
148                 egid => $),
149             );
150             $class->SUPER::new(%args);
151         }
152
153         eval {
154             HackException->throw;
155         };
156         my $e = $@;
157         say $e->time;
158         say $e->pid;
159         say $e->uid;
160         say $e->euid;
161         say $e->gid;
162         say $e->egid;
163

AUTHOR

165       Kazuhiro Osawa <yappo {@} shibuya {dot} pl>
166

SEE ALSO

168       Class::Accessor::Lite
169

LICENSE

171       Copyright (C) Kazuhiro Osawa
172
173       This library is free software; you can redistribute it and/or modify it
174       under the same terms as Perl itself.
175
176
177
178perl v5.34.0                      2021-07-22                Exception::Tiny(3)
Impressum