Can a try/except in a file catch a syntax error in the same file?
Syntax errors are detected at compile time.
Exception Handling · Exercise 1 of 4
Fifteen multiple-choice questions on Python exception handling: try, except, else, finally, the hierarchy, raise, custom exceptions, chaining and good practice. Pick an option to check your answer instantly.
Can a try/except in a file catch a syntax error in the same file?
Syntax errors are detected at compile time.
Why should assert not be used to validate user input?
Validate input with if and raise ValueError; keep assert for internal checks.
What does the EAFP coding style mean?
"Easier to Ask Forgiveness than Permission" – the Pythonic alternative to Look Before You Leap.
What happens to the name e after the block except ValueError as e: ends?
Python deletes it to avoid a reference cycle with the traceback. Assign it to another name if you need it later.
What happens with raise "Invalid input"?
Only exception classes or instances can be raised: raise ValueError("Invalid input").
Timed online tests with instant scores and explanations.