Not really, because it silently hides bugs. Better habits:
Catch specific exceptions you know how to handle: except FileNotFoundError: or except ValueError:.
Keep the try block small, only the line that can fail.
Do something useful in except: log the error, show a message, retry or clean up. Never use a bare except: or except Exception: pass.
Use finally or, better, with (context managers) for cleanup such as closing files.
Use else for code that should run only if no exception happened.
raise again if you cannot handle it. Use raise ... from err to keep the original cause.
try:
value = int(text)
except ValueError:
print("Please enter a number")
For unexpected errors, let the program fail loudly or log them with the traceback (logging.exception). A crash you can see is better than a bug you cannot.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Not really, because it silently hides bugs. Better habits:
except FileNotFoundError:orexcept ValueError:.except:orexcept Exception: pass.raise ... from errto keep the original cause.For unexpected errors, let the program fail loudly or log them with the traceback (
logging.exception). A crash you can see is better than a bug you cannot.