Search code examples
windowsexceptionpython-3.3

Catching TimeoutExpired exception in Python 3.3


Sorry if this is a newbie question, but I'm having trouble catching the timeout exception in Python 3.3, running on win7, e.g.

import subprocess
try:
   subprocess.call("ping -t localhost", timeout=3)
except TimeoutExpired:
    print("Timeout happened.\n")

The timeout works fine, and according to my pdb traceback, it says: "raise TimeoutExpired(self.args, timeout)"

yet "except TimeoutExpired:" doesn't catch it. Also, TimeoutExpired is not listed as a standard exception and comes out as a nameError.

If I try "except TimeoutError:" instead of "except TimeoutExpired:", I don't get the error message, but in pdb, I get:

"Uncaught exception"

and the print command does not get executed in any case.

Is this a bug, or am I doing something wrong?


Solution

  • TimeoutExpired is not globally defined; use subprocess.TimeoutExpired instead.