1 package XML::SAX::Exception;
5 use overload '""' => "stringify",
8 use vars qw/$StackTrace $VERSION/;
12 $StackTrace = $ENV{XML_DEBUG} || 0;
14 # Other exception classes:
16 @XML::SAX::Exception::NotRecognized::ISA = ('XML::SAX::Exception');
17 @XML::SAX::Exception::NotSupported::ISA = ('XML::SAX::Exception');
18 @XML::SAX::Exception::Parse::ISA = ('XML::SAX::Exception');
32 confess "Invalid options: " . join(', ', keys %opts) unless exists $opts{Message};
34 bless { ($StackTrace ? (StackTrace => stacktrace()) : ()), %opts },
42 if (exists $self->{LineNumber}) {
43 $error = $self->{Message} . " [Ln: " . $self->{LineNumber} .
44 ", Col: " . $self->{ColumnNumber} . "]";
47 $error = $self->{Message};
50 $error .= stackstring($self->{StackTrace});
59 while (my @trace = caller($i++)) {
61 @hash{qw(Package Filename Line)} = @trace[0..2];
62 push @fulltrace, \%hash;
68 my $stacktrace = shift;
69 my $string = "\nFrom:\n";
70 foreach my $current (@$stacktrace) {
71 $string .= $current->{Filename} . " Line: " . $current->{Line} . "\n";
82 XML::SAX::Exception - Exception classes for XML::SAX
86 throw XML::SAX::Exception::NotSupported(
87 Message => "The foo feature is not supported",
92 This module is the base class for all SAX Exceptions, those defined in
93 the spec as well as those that one may create for one's own SAX errors.
95 There are three subclasses included, corresponding to those of the SAX
98 XML::SAX::Exception::NotSupported
99 XML::SAX::Exception::NotRecognized
100 XML::SAX::Exception::Parse
102 Use them wherever you want, and as much as possible when you encounter
103 such errors. SAX is meant to use exceptions as much as possible to
106 =head1 CREATING NEW EXCEPTION CLASSES
108 All you need to do to create a new exception class is:
110 @XML::SAX::Exception::MyException::ISA = ('XML::SAX::Exception')
112 The given package doesn't need to exist, it'll behave correctly this
113 way. If your exception refines an existing exception class, then you
114 may also inherit from that instead of from the base class.
116 =head1 THROWING EXCEPTIONS
118 This is as simple as exemplified in the SYNOPSIS. In fact, there's
119 nothing more to know. All you have to do is:
121 throw XML::SAX::Exception::MyException( Message => 'Something went wrong' );
123 and voila, you've thrown an exception which can be caught in an eval block.