I want to catch every program error so that I can display those errors in my GUI program, but I find I'm not able to catch certain kinds of error like SyntaxError and IndentError.
For example:
import traceback
zero = 0
try:
print "start..."
v = 3/zero # deliberately no indention, SyntaxError cannot be caught
except:
print "Oooooooooooooops"
traceback.print_exc()
exit(1)
print "run ok"
The console output is:
File "D:\w\personal\chj\python-try\catch-syntaxerror\catch_syntax_err.py", line 8
v = 3/zero # ``SyntaxError: invalid syntax``, cannot be catched by user
^
SyntaxError: invalid syntax
So, I know I did not catch the exception myself.
How can I catch it?