OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Re: [xml-dev] SAXException, checked, buy why?

[ Lists Home | Date Index | Thread Index ]

On Sun, 2005-01-02 at 21:15 -0500, Alan Gutierrez wrote:
> * Uche Ogbuji <uche.ogbuji@fourthought.com> [2005-01-02 20:33]:
> 
> > On Sun, 2005-01-02 at 19:46 -0500, Alan Gutierrez wrote:
> 
> > >     I'm hoping that I'm not talking to myself on this one. If this
> > >     is too Java oriented, tell me, but I'd like to sort this out so
> > >     that maybe I can create something worth sharing.
> 
> > It may be Java-oriented because I don't understand the problem.
> > Speaking for the Python case, since you don't have to declare
> > exceptions, the handler can throw anything it pleases, which, if not
> > handled by the driver falls to the original parse-calling code.
> 
>     I'm not Python-oriented, so I might be making hash of this
>     response, but I think this is a generic concern.

Oh.  It may be.  See below to determine whether I have actually shown
it's not a problem for very dynamic languages.


>     Assume no checked exceptions. That is some Java hand-wringing
>     that I'll discuss with Java developers if they are interested.
>     
>     <java-specific-musing boring-for="Uche">

Well, there are a lot more other-than-Java programmers than just me :-)


>         Asking: "How do you provide the fellow who invokes the event
>         conduit with a means to pitch an exception that they will
>         know to catch?" of a Python programmer is begging for flames.

Sorry.  I peeked here.  Surely you didn't construe my last response as a
flame, right?  Just an honest quest for clarification.


>     How do you provide the fellow who invokes the event conduit with
>     a means to intercept exceptions at the event handler?
>     
>     Say I've a content handler that likes to open files. The action
>     to take when a file is not found could be to skip it an move on,
>     or it could be that the file can be fabricated real quick, or it
>     could be that processing has to abort via an exception.
>     
>     This is an application descision. How does the application
>     developer configure the content handler so that the exceptional
>     condition is handled during the event?

Well, my Java-Fu is decent enough to know that this is really just not
an option in Java, but I'll go on, since I think I understand you'd be
interested even in a non-Java answer:

In Python, Exceptions are just as available for dynamic processing as
any other thing.  Here is an example:

l = [1,2,3] #list (like a tuple, but mutable)
 #tuple of exception classes
exceptions_to_ignore = (IndexError, AttributeError)
try:
    l[4]  #This will raise an IndexError
except exceptions_to_ignore:
    pass  #This is Python idiom for ignoring the exception

So here we are dynamically specifying the exceptions that should be
handled in a certain way.

More:

l = [1,2,3]
exceptions_to_ignore = (IndexError, ) #Syntax for tuple-of-one
exceptions_to_just_print_error = (AttributeError, )
try:
    l.foo  #Will raise an AttributeError: lists have no 'foo' property
except exceptions_to_ignore:
    pass  #This is Python idiom for ignoring the exception
#In this form e gets instantiated with the actual exception caught
except exceptions_to_just_print_error, exc:
    print "Exception raised", exc

In the above case, the last line will end up being fired

So this would be easy to apply to your scenario:

MyHandler(xml.sax.ContentHandler):
    #Following method is the initializer (like a constructor)
    def __init__(self, exceptions_to_ignore, exceptions_to_pass_on,
                 exceptions_to_log):
        self.exceptions_to_ignore = exceptions_to_ignore
        self.exceptions_to_pass_on = exceptions_to_pass_on
        self.exceptions_to_log = exceptions_to_log
        self.count = 0

    def characters(self, text):
        #Crazy example: put each text chunk into a 
        try:
            #May raise IOError
            f = open(str(self.count) + '.txt')
            f.write(text.encode('utf-8'))
            #May raise IndexErrors for small strings
            f.write(text[10])  #May raise 
            #May raise UnicodeEncodeError if text contains 
            #Characters above ASCII range (U+127)
            f.write(text.encode('ascii'))
            count += 1
        except self.exceptions_to_ignore:
            pass
        except self.exceptions_to_pass_on:
            raise  #Python idiom for re-throwing the exception
        except self.exceptions_to_log, exc:
            print "Exception raised", exc


parser = xml.sax.make_parser()
#Specify that IOErrors be ignored, IndexErrors passed to the
#parser-invoking code, and UnicodeEncodeError causes a print-out
handler = MyHandler((IOError, ),
                    (IndexError, )
                    (UnicodeEncodeError, ))

parser.setContentHandler(handler)
try:
    parser.parse() #This will now catch all IndexErrors
except IndexError:
    print "Hey! A char data chunks is less than 10 characters" 

I think this provides just about all the flexibility one might want in
marshalling exceptions, FWIW.


-- 
Uche Ogbuji                                    Fourthought, Inc.
http://uche.ogbuji.net    http://4Suite.org    http://fourthought.com
Use CSS to display XML - http://www.ibm.com/developerworks/edu/x-dw-x-xmlcss-i.html
Full XML Indexes with Gnosis - http://www.xml.com/pub/a/2004/12/08/py-xml.html
Be humble, not imperial (in design) - http://www.adtmag.com/article.asp?id=10286
UBL 1.0 - http://www-106.ibm.com/developerworks/xml/library/x-think28.html
Use Universal Feed Parser to tame RSS - http://www.ibm.com/developerworks/xml/library/x-tipufp.html
Default and error handling in XSLT lookup tables - http://www.ibm.com/developerworks/xml/library/x-tiplook.html
A survey of XML standards - http://www-106.ibm.com/developerworks/xml/library/x-stand4/
The State of Python-XML in 2004 - http://www.xml.com/pub/a/2004/10/13/py-xml.html





 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS