Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | # $Id: Exception.pm,v 1.3 2003/04/29 18:18:05 malay Exp $\r |
2 | # Perl module for Pastel::Exception\r | |
3 | # Author: Malay < curiouser@ccmp.ap.nic.in >\r | |
4 | # Copyright Malay\r | |
5 | # You may distribute this module under the same terms as perl itself\r | |
6 | \r | |
7 | # POD documentation - main docs before the code\r | |
8 | \r | |
9 | =head1 NAME\r | |
10 | \r | |
11 | Pastel::Exception - A module for easy exception handling in Pastel.\r | |
12 | \r | |
13 | =head1 SYNOPSIS\r | |
14 | \r | |
15 | #For thowing exception from any module\r | |
16 | \r | |
17 | use Pastel;\r | |
18 | # Creating an exception object has the advantage of calling \r | |
19 | # the full stack trace. If the catch() method in the Pastel::Exception\r | |
20 | # class is not overridden, throwing an exception with a Exception object\r | |
21 | # as parameter will call the full stack trace.\r | |
22 | \r | |
23 | $self->throw(Pastel::Exception("Miserable condition"));\r | |
24 | \r | |
25 | or,\r | |
26 | \r | |
27 | $self->throw("Miserable condition"); \r | |
28 | # If not caught it just calls croak "Miserable condition"\r | |
29 | \r | |
30 | or,\r | |
31 | \r | |
32 | $self->throw(); \r | |
33 | # If not caught it just calls croak "Unknown error";\r | |
34 | \r | |
35 | \r | |
36 | # Catching an exception from a module\r | |
37 | # You need to overide the catch() of Exception class\r | |
38 | \r | |
39 | $self->throw(Pastel::Exception("Some error");\r | |
40 | \r | |
41 | sub catch {\r | |
42 | my ($self, $exception) = @_;\r | |
43 | $exception->print_stack_trace(); # Give full stack trace\r | |
44 | print $exception->get_message(); # prints "Some error"\r | |
45 | croak "Miserable death\n";\r | |
46 | } \r | |
47 | \r | |
48 | \r | |
49 | \r | |
50 | =head1 DESCRIPTION\r | |
51 | \r | |
52 | Pastel::Exception is the root class of handling exception in Pastel. There are two ways exception can be handled. Either inheriting from Pastel::Exception andoverriding catch() method, or by simply Creating a Pastel::Exception object and using its throw method. The exception is caught automatically by the Pastel::Exception::catch(). \r | |
53 | \r | |
54 | =head1 CONTACT\r | |
55 | \r | |
56 | Malay <curiouser@ccmb.ap.nic.in>\r | |
57 | \r | |
58 | =cut\r | |
59 | \r | |
60 | # Let the code begin...\r | |
61 | \r | |
62 | package Pastel::Exception;\r | |
63 | use Carp;\r | |
64 | use strict;\r | |
65 | \r | |
66 | =head1 CONSTRUCTOR\r | |
67 | \r | |
68 | =head2 new($string)\r | |
69 | \r | |
70 | Usage : Pastel::Exception->new("Some error");\r | |
71 | or, Pastel::Exception->new();\r | |
72 | Function : Creates and returns a Pastel::Exception object\r | |
73 | Returns : Pastel::Exception object\r | |
74 | Arguments: A string (optional);\r | |
75 | \r | |
76 | =cut\r | |
77 | \r | |
78 | sub new {\r | |
79 | my ( $class, $string ) = shift;\r | |
80 | \r | |
81 | my $self = {};\r | |
82 | bless $self, ref($class) || $class;\r | |
83 | if ($string) {\r | |
84 | $self->{message} = $string;\r | |
85 | }\r | |
86 | else {\r | |
87 | $self->{message} = "Unknown error";\r | |
88 | }\r | |
89 | return $self;\r | |
90 | }\r | |
91 | \r | |
92 | =head1 METHODS\r | |
93 | \r | |
94 | =head2 throw()\r | |
95 | \r | |
96 | Usage: $self->throw($e); # Inherited from Pastel::Exception class\r | |
97 | or, $self->throw();\r | |
98 | or, $self->throw($string);\r | |
99 | Function: Throws an exception.\r | |
100 | Returns: Nothing \r | |
101 | Arguments: Either an Pastel::Exception object or a string or nothing\r | |
102 | \r | |
103 | =cut\r | |
104 | \r | |
105 | sub throw {\r | |
106 | my ( $self, $e ) = @_;\r | |
107 | if ( $self->can('catch') ) {\r | |
108 | $self->catch($e);\r | |
109 | }\r | |
110 | }\r | |
111 | \r | |
112 | =head2 catch()\r | |
113 | \r | |
114 | Usage : Should never be directly used. To use this function, override \r | |
115 | this function after inheriting from Pastel::Exception. \r | |
116 | Function : Catches an exception\r | |
117 | Returns : Nothing\r | |
118 | Arguments: The function is always called with the same parameter when thrown.\r | |
119 | May be a string or an Exception object or null. Should always be \r | |
120 | tested with isa() before use.\r | |
121 | \r | |
122 | =cut\r | |
123 | \r | |
124 | sub catch {\r | |
125 | my ( $self, $e ) = @_;\r | |
126 | print STDERR "\nException reached the root exception class!\n";\r | |
127 | if ( defined($e) && $e->isa('Pastel::Exception') ) {\r | |
128 | $e->print_stack_trace();\r | |
129 | croak "\n", $e->get_message(), "\n";\r | |
130 | }\r | |
131 | elsif ( defined($e) ) {\r | |
132 | $self->print_stack_trace();\r | |
133 | croak $e, "\n";\r | |
134 | }\r | |
135 | else {\r | |
136 | croak "Unknown error!\n";\r | |
137 | }\r | |
138 | \r | |
139 | }\r | |
140 | \r | |
141 | =head2 print_stack_trace()\r | |
142 | \r | |
143 | Usage : $exception_object->print_stack_trace(); \r | |
144 | Function : Prints complete stack trace of the call.\r | |
145 | Returns : Nothing\r | |
146 | Arguments: Nothing\r | |
147 | \r | |
148 | =cut\r | |
149 | \r | |
150 | \r | |
151 | sub print_stack_trace {\r | |
152 | my $self = shift;\r | |
153 | \r | |
154 | #my(@a) = caller();\r | |
155 | #print "@a", "\n";\r | |
156 | print STDERR "\n--------------------------------------------\n";\r | |
157 | print STDERR " STACK TRACE \n";\r | |
158 | print STDERR "--------------------------------------------\n";\r | |
159 | \r | |
160 | my $i = 0;\r | |
161 | while (1) {\r | |
162 | my (@b) = caller($i);\r | |
163 | if ( !@b ) {\r | |
164 | \r | |
165 | last;\r | |
166 | }\r | |
167 | my @c = ();\r | |
168 | foreach my $element (@b) {\r | |
169 | if ($element) {\r | |
170 | $c[@c] = $element;\r | |
171 | }\r | |
172 | }\r | |
173 | print STDERR "Package : $c[0]\n";\r | |
174 | print STDERR "File : $c[1]\n";\r | |
175 | print STDERR "Line No : $c[2]\n";\r | |
176 | if ( $c[3] ) {\r | |
177 | print STDERR "Function : $c[3]\n";\r | |
178 | }\r | |
179 | print STDERR "--------------------------------------------\n";\r | |
180 | $i++;\r | |
181 | }\r | |
182 | \r | |
183 | #print "@b", "\n";\r | |
184 | }\r | |
185 | \r | |
186 | \r | |
187 | =head2 get_message()\r | |
188 | \r | |
189 | Usage : $exception_object->get_message() \r | |
190 | Function : Return the message stored in the exception object.\r | |
191 | Returns : A scalar containg the massage string.\r | |
192 | Arguments: Nothing\r | |
193 | \r | |
194 | =cut\r | |
195 | \r | |
196 | \r | |
197 | sub get_message {\r | |
198 | return $_[0]->{message};\r | |
199 | }\r | |
200 | 1;\r |