0

I have following code, in which i have to handle exception for 2 statements,

2nd line and 4th line

if(re.search("USN:.*MediaRenderer", datagram, flags=re.IGNORECASE)):
    deviceXML = re.search("LOCATION:(.*.xml)", datagram, flags=re.IGNORECASE).group(1)   # this line            
    root = ElementTree.fromstring(urllib2.urlopen(XMLLocation).read())                
    friendlyName = root.find('.//{}friendlyName'.format(Server.namespace)).text   # this line
    if not friendlyName in deviceList.keys():
       deviceList[friendlyName] = host
    self.model.setStringList(deviceList.keys())

How can i use nested try/catch here

I tried following way:

if(re.search("USN:.*MediaRenderer", datagram, flags=re.IGNORECASE)):
        try:
            deviceXML = re.search("LOCATION:(.*.xml)", datagram, flags=re.IGNORECASE).group(1)            
            root = ElementTree.fromstring(urllib2.urlopen(XMLLocation).read())                
            try:
                friendlyName = root.find('.//{}friendlyName'.format(Server.namespace)).text
                print "\n fname = ", friendlyName
                if not friendlyName in deviceList.keys():
                    deviceList[friendlyName] = host
                self.model.setStringList(deviceList.keys())                
        except:
            pass

This is giving me indentation error for except line

1 Answer 1

4

Your inner try block is missing an except clause (which is required).

try:
    # do something risky

    try:
        # do another risky thing
    except:  # <-- this is required
        # handle the inner exception

except Exception as exc:
    # handle outer exception

But you may want to restructure your code to have two separate blocks. It will be cleaner that way and easier to understand/maintain.

if(re.search("USN:.*MediaRenderer", datagram, flags=re.IGNORECASE)):
    try:
        deviceXML = re.search("LOCATION:(.*.xml)", datagram, flags=re.IGNORECASE).group(1)            
        root = ElementTree.fromstring(urllib2.urlopen(XMLLocation).read())                

    except:
        # return, break, etc.

    # no exception from previous block; proceed with processing

    try:
        friendlyName = root.find('.//{}friendlyName'.format(Server.namespace)).text
        print "\n fname = ", friendlyName
        if not friendlyName in deviceList.keys():
            deviceList[friendlyName] = host
        self.model.setStringList(deviceList.keys())                

    except Exception as exc:
        # do something with the error here
Sign up to request clarification or add additional context in comments.

2 Comments

having 2 except is not giving me any o/p
Okay. Can't help you with no output unless you post your code. (But this answer illustrates how to use nested exceptions.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.