Help catching error message

Jean-Michel Pichavant jeanmichel at sequans.com
Tue Nov 8 12:15:40 EST 2011


Gnarlodious wrote:
> What I say is this:
>
> def SaveEvents(self,events):
>    try:
>       plistlib.writePlist(events, self.path+'/Data/Events.plist') #
> None if OK
>    except IOError:
>       return "IOError: [Errno 13] Apache can't write Events.plist
> file"
>
> Note that success returns"None" while failure returns a string.
>
> I catch the error like this:
>
> errorStatus=Data.Dict.SaveEvents(Data.Plist.Events)
> if errorStatus: content=errorStatus
>
> It works, but isn there a more elegant way to do it? As in, one line?
> I can imagine if success returned nothing then content would remain
> unchanged. Isn't there a built-in way to send an error string back and
> then catch it as a variable name?
>
> This is Py3 inside WSGI.
>
> -- Gnarlie
>   
Hi,

There's no need to rephrase an exception unless you want to *add* 
information.

def saveEvents(self,events):
    plistlib.writePlist(events, self.path+'/Data/Events.plist') 

try:   
    Data.Dict.SaveEvents(Data.Plist.Events)
except IOError, exc: # i'm using python 2.5, this except clause may have 
changed in py3
    content = str(exc)

JM




More information about the Python-list mailing list