How to except SyntaxError?
问题 I would like to except the error the following code produces, but I don't know how. from datetime import datetime try: date = datetime(2009, 12a, 31) except: print "error" The code above is not printing "error" . That's what I would like to be able to do. edit: The reason I would like to check for syntax errors, is because 12a is a command line parameter. Thanks. 回答1: command-line "parameters" are strings. if your code is: datetime(2009, '12a', 31) it won't produce SyntaxError . It raises