3 Replies - 1151 Views - Last Post: 06 November 2012 - 05:37 AM Rate Topic: -----

#1 zedth2   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 121
  • Joined: 14-September 09

Collapse multiple try except blocks

Posted 05 November 2012 - 05:47 PM

So I'm just writing a little function that'll read an XML element and the information from it.

def GetXML(self, Elem):
        if issubclass(type(Elem), ElementTree.Element):
            try:
                self.Title = Elem.attrib['Title']
            except KeyError:
                pass
            try:
                self.Content = Elem.find('Content').text.strip()
            except AttributeError:
                pass
            try:
                self.Date = Elem.attrib['Date']
            except KeyError:
                pass
        else:
            raise TypeError("Elem must be of type xml.etree.ElementTree.Element")




I'm using Python 3.2 and if the element or attribute doesn't exist it's no big deal we can keep working so I just pass through the errors. But my question is there a way to put these into one try except block that if one line throws the error it just keeps going through the next one?
So something like this, I know this wouldn't work but just to try and explain what I'm looking for...

            try:
                self.Title = Elem.attrib['Title']
                self.Content = Elem.find('Content').text.strip()
                self.Date = Elem.attrib['Date']
            except (AttributeError, KeyError):
                continue



Does anything like this exist in python?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Collapse multiple try except blocks

#2 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 837
  • View blog
  • Posts: 2,271
  • Joined: 23-December 08

Re: Collapse multiple try except blocks

Posted 05 November 2012 - 07:31 PM

It sure does:
try:
    x = []
    x[0] = 5
    print(x)
except (KeyError, IndexError) as error:
    print(error)


Was This Post Helpful? 0
  • +
  • -

#3 zedth2   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 121
  • Joined: 14-September 09

Re: Collapse multiple try except blocks

Posted 05 November 2012 - 07:58 PM

That doesn't work. If we use your code as an example what I would like to see is print(error) then print(x) but all I see is print(error).
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2770
  • View blog
  • Posts: 4,429
  • Joined: 21-June 11

Re: Collapse multiple try except blocks

Posted 06 November 2012 - 05:37 AM

No such thing exists in Python (to the best of my knowledge). I think in your case the best solution would seem to be to simply avoid throwing an exception in the first place.

The KeyErrors can easily be avoided by using get instead of [], which returns None if the key was not found. Of course this will lead to different behavior from your original code because now the attributes will be set to None when the key is not found instead of not being set at all - but hopefully that difference won't matter to your use case.

To avoid the AttributeError, you'll probably have to use an if that checks whether find returned None.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1