Why can I catch a SyntaxError (or IndentationError or TabError) raised by eval'd code, but not caused by the source code itself?
13:15 06 Dec 2009

Consider these two snippets:

try:
    a+a=a
except SyntaxError:
    print("first exception caught")

and

try:
    eval("a+a=a")
except SyntaxError:
    print("second exception caught")

In the second case the "second exception .." statement is printed (exception caught), while in the first one isn't.

Is first exception (lets call it "SyntaxError1") any different from second one ("SyntaxError2")?

Is there any way to catch SyntaxError1 (thus supressing compilation-time errors)? Wrapping large blocks of code in eval is unsatisfactory ;)

python exception