3 ms·
SyntaxError can happen as a consequence of loading a module (hitting an import statement, etc). But for a simpler example, replace SyntaxError with NameError a
by wyldfire 11y ago
SyntaxError can happen as a consequence of loading a module (hitting an import statement, etc). But for a simpler example, replace SyntaxError with NameError and it's still almost always a design error that you probably have no sane way to handle in an exception handler (other than to mask the problem).
Python's a very dynamic language and so of course you could come up with examples to the contrary (ones where the user input could trigger SyntaxError or NameError).
- techdragon 11y agoBut the only universally good reason I know of where you should be wrapping imports in try except is 2/3 compatibility or other module/library import fallback. For these you need to be catching syntaxerror and other system errors.