Missing something on exception handling in Python 3

Ethan Furman ethan at stoneleaf.us
Mon Aug 26 23:16:00 EDT 2013


On 08/26/2013 07:49 PM, Skip Montanaro wrote:
>> Do this:
>>
>>      raise LockFailed("Failed to create %s" % self.path) from None
>
> Thanks. Is there some construct which will work in 2.x and 3.x?

Something like this (untested):

     exc = None
     try:
         write_pid_to_lockfile(somefile)
     except OSError as exc:
         exc = sys.exc_info()[0]   # not sure of index
     if exc:
         if conditions_i_can_handle:
             do_other_stuff...
         else:
             raise LockFailed("Failed to create %s" % self.path)

Since you're raising outside the try...except block you won't see the chained exception in Python3.

--
~Ethan~



More information about the Python-list mailing list