Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
try holds code that may fail, except handles matching exceptions, else runs only if no exception occurred, and finally always runs, even after return or an unhandled error.
Put only the code that can raise the expected exception inside try, and catch specific exception types in except so unrelated bugs are not hidden. The else block is for code that should run only when try succeeded, which keeps try small. finally runs whether the block succeeded, failed or returned early, so it is used for cleanup like closing files or releasing locks, although a with statement is usually cleaner. A bare except: or except Exception: pass swallows real errors and makes debugging very hard.