1Mojo::Exception(3) User Contributed Perl Documentation Mojo::Exception(3)
2
3
4
6 Mojo::Exception - Exceptions with context
7
9 use Mojo::Exception;
10
11 # Throw exception and show stack trace
12 eval { Mojo::Exception->throw('Something went wrong!') };
13 say "$_->[1]:$_->[2]" for @{$@->frames};
14
15 # Customize exception
16 eval {
17 my $e = Mojo::Exception->new('Died at test.pl line 3.');
18 die $e->trace(2)->inspect->verbose(1);
19 };
20 say $@;
21
23 Mojo::Exception is a container for exceptions with context information.
24
26 Mojo::Exception implements the following attributes.
27
28 frames
29 my $frames = $e->frames;
30 $e = $e->frames([$frame1, $frame2]);
31
32 Stack trace if available.
33
34 # Extract information from the last frame
35 my ($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext,
36 $is_require, $hints, $bitmask, $hinthash) = @{$e->frames->[-1]};
37
38 line
39 my $line = $e->line;
40 $e = $e->line([3, 'die;']);
41
42 The line where the exception occurred if available.
43
44 lines_after
45 my $lines = $e->lines_after;
46 $e = $e->lines_after([[4, 'say $foo;'], [5, 'say $bar;']]);
47
48 Lines after the line where the exception occurred if available.
49
50 lines_before
51 my $lines = $e->lines_before;
52 $e = $e->lines_before([[1, 'my $foo = 23;'], [2, 'my $bar = 24;']]);
53
54 Lines before the line where the exception occurred if available.
55
56 message
57 my $msg = $e->message;
58 $e = $e->message('Died at test.pl line 3.');
59
60 Exception message, defaults to "Exception!".
61
62 verbose
63 my $bool = $e->verbose;
64 $e = $e->verbose($bool);
65
66 Enable context information for "to_string".
67
69 Mojo::Exception inherits all methods from Mojo::Base and implements the
70 following new ones.
71
72 inspect
73 $e = $e->inspect;
74 $e = $e->inspect($source1, $source2);
75
76 Inspect "message", "frames" and optional additional sources to fill
77 "lines_before", "line" and "lines_after" with context information.
78
79 new
80 my $e = Mojo::Exception->new;
81 my $e = Mojo::Exception->new('Died at test.pl line 3.');
82
83 Construct a new Mojo::Exception object and assign "message" if
84 necessary.
85
86 to_string
87 my $str = $e->to_string;
88
89 Render exception.
90
91 # Render exception with context
92 say $e->verbose(1)->to_string;
93
94 throw
95 Mojo::Exception->throw('Something went wrong!');
96
97 Throw exception from the current execution context.
98
99 # Longer version
100 die Mojo::Exception->new('Something went wrong!')->trace->inspect;
101
102 trace
103 $e = $e->trace;
104 $e = $e->trace($skip);
105
106 Generate stack trace and store all "frames", defaults to skipping 1
107 call frame.
108
109 # Skip 3 call frames
110 $e->trace(3);
111
112 # Skip no call frames
113 $e->trace(0);
114
116 Mojo::Exception overloads the following operators.
117
118 bool
119 my $bool = !!$e;
120
121 Always true.
122
123 stringify
124 my $str = "$e";
125
126 Alias for "to_string".
127
129 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
130
131
132
133perl v5.28.0 2018-10-16 Mojo::Exception(3)