Code:
require "rubygems"
require "xml"
class TaskLoader
class BadXML < RuntimeError
end
def load_tasks
doc = XML::Parser.file( "qq.xml" ).parse
# ... work with document ...
rescue XML::Parser::ParseError, BadXML # this is line #40
# ...
end
end # TaskLoader
p TaskLoader.new.load_tasks # this is line #71
Output:
read_xml.rb:40:in `load_tasks'': uninitialized constant
LibXML::XML::Parser::ParseError (NameError)
from read_xml.rb:71
Question: why does ruby interpret XML::Parser::ParseError in rescue-
clause as LibXML::XML::Parser::ParseError?
Thanks.
Frederick Cheung
2009-Sep-04 14:12 UTC
Re: libxml-ruby: How to handle XML::Parser::ParseError?
On Sep 4, 2:41 pm, adrianopol <adriano...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> read_xml.rb:40:in `load_tasks'': uninitialized constant > LibXML::XML::Parser::ParseError (NameError) > from read_xml.rb:71 > > Question: why does ruby interpret XML::Parser::ParseError in rescue- > clause as LibXML::XML::Parser::ParseError?because the top level constant XML is LibXML::XML (check their object_id ) Fred> Thanks.
OK, so, how should I handle this exception? On Sep 4, 6:12 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sep 4, 2:41 pm, adrianopol <adriano...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > read_xml.rb:40:in `load_tasks'': uninitialized constant > > LibXML::XML::Parser::ParseError (NameError) > > from read_xml.rb:71 > > > Question: why does ruby interpret XML::Parser::ParseError in rescue- > > clause as LibXML::XML::Parser::ParseError? > > because the top level constant XML is LibXML::XML (check their > object_id ) > > Fred > > > Thanks.
Documentation (http://libxml.rubyforge.org/rdoc/classes/LibXML/XML/
Parser.html#M000123, bottom of the page) says that
XML::Parser::ParseError should be thrown. I tried to raise it (trying
to parse bad xml-string) and the exception is really different:
irb(main):005:0> XML::Parser.string(
"<foo></ba/r/></foo>").parse
Fatal error: expected ''>'' at :1.
Fatal error: Opening and ending tag mismatch: foo line 1 and ba
at :1.
Fatal error: Extra content at the end of the document at :1.
LibXML::XML::Error: Fatal error: Extra content at the end of the
document at :1.
from (irb):5:in `parse''
from (irb):5
from (null):0
I think, i should handle LibXML::XML::Error instead. The problem is
solved.
On Sep 4, 6:18 pm, adrianopol
<adriano...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> OK, so, how should I handle this exception?
>
> On Sep 4, 6:12 pm, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> wrote:
>
> > On Sep 4, 2:41 pm, adrianopol
<adriano...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > > read_xml.rb:40:in `load_tasks'': uninitialized constant
> > > LibXML::XML::Parser::ParseError (NameError)
> > > from read_xml.rb:71
>
> > > Question: why does ruby interpret XML::Parser::ParseError in
rescue-
> > > clause as LibXML::XML::Parser::ParseError?
>
> > because the top level constant XML is LibXML::XML (check their
> > object_id )
>
> > Fred
>
> > > Thanks.