How can I capture all exceptions especially when os.system() fail? Thanks
mike
needpassion at gmail.com
Wed Jun 13 20:47:16 EDT 2007
Hi Guys,
Following piece of code can capture IOError when the file doesn't
exist, also, other unknown exceptions can be captured when I press
Ctrl-C while the program is sleeping(time.sleep). Now the question is:
when I run the non-exist command, the exception cannot be captured.
Here is the code:
===================================
#!/usr/bin/python
import os
import sys
import time
try:
fh = open("tt.py")
time.sleep(10)
#os.system("wrong_command_test")
except IOError:
print 'failed to open.'
sys.exit(0)
except:
print 'Some exceptions occurred.'
else:
print 'well',
print 'Done'
===================================
when the tt.py doesn't exist, the script printed:
failed to open.
when the tt.py exists, the script printed:
well done
when I press Ctrl-C while the program is sleeping, the script printed:
Some exceptions occurred.
Done
So far so good, then I changed the code to run a non-exist command
"wrong_command_test"(commented the open and sleep lines), then the
script printed:
sh: wrong_command_test: command not found
well Done
Any opinions would be appreciated.
Mike
More information about the Python-list
mailing list