Robust exception handling (in Python) - a "best practices for Python exceptions" blog post I wrote a while ago. You may find it useful.

Some key points from the blog:

Never use exceptions for flow-control

Exceptions exist for exceptional situations: events that are not a part of normal execution.

Consider 'find' on a string returning -1 if the pattern isn't found, but indexing beyond the end of a string raises an exception. Not finding the string is normal execution.

Handle exceptions at the level that knows how to handle them

...

The best place is that piece of code that can handle the exception. For some exceptions, like programming errors (e.g. IndexError, TypeError, NameError etc.) exceptions are best left to the programmer / user, because "handling" them will just hide real bugs.

Always ask "is this the right place to handle this exception?" and be careful with catching all exceptions.

Document the exceptions thrown by your code

...

thinking about which exceptions your code may throw will help you write better, safer and more encapsulated code

Answer from Eli Bendersky on Stack Overflow
🌐
Miguel Grinberg
blog.miguelgrinberg.com › post › the-ultimate-guide-to-error-handling-in-python
The Ultimate Guide to Error Handling in Python - miguelgrinberg.com
The application can simply let errors bubble up, and they'll eventually be caught here, where the error message will be shown and the application will then exit with an error code. You may remember that I've mentioned above that catching all exceptions is a bad practice. Yet, that is exactly what I'm doing here! The reason is that at this level we really cannot let any exceptions reach Python because we do not want this program to ever crash, so this is the one situation in which it makes sense to catch all exceptions.
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.7 documentation
If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops with an error message.
Discussions

Best Practices for Python Exceptions? - Stack Overflow
I recommend something like this ... -> Error -> GeneralError -> BaseError -> WhyIsThisHappeningerror. ... The linked duplicate has a far more productive question, answer and discussion: https://stackoverflow.com/questions/1319615/proper-way-to-declare-custom-exceptions-in-modern-python ... Save this answer. ... Show activity on this post. Robust exception handling (in Python) - a "best practices for Python ... More on stackoverflow.com
🌐 stackoverflow.com
Confused about the best practices for error handling
It depends what you mean by "end user". If a program is meant to be used by a non-programmer (like a command-line script utility, or anything with a GUI), then absolutely, you should try to make it as user-friendly as possible (the full error traceback isn't going to be particularly useful to them). But if you're writing code that other Python programmers are going to use in their own projects, you want to stick to the conventions of error handling that everyone is used to. That then gives those other programmers the control to handle/display the error however they see fit, depending on who their end users are going to be. More on reddit.com
🌐 r/learnpython
5
0
January 15, 2023
Python errors as values: Comparing useful patterns from Rust and Go
I like Go but this error handling repeat itself everywhere. Prefer the exceptions More on reddit.com
🌐 r/Python
8
17
November 9, 2023
Would you use Go / Rust style error handling in Python?
What's the benefit of having function 1 return an error string which function 2 raises as an exception? Instead of just having function 1 raise the exception More on reddit.com
🌐 r/Python
52
0
March 13, 2024
🌐
Reddit
reddit.com › r/learnpython › exception handling best practices?
r/learnpython on Reddit: Exception Handling Best Practices?
November 17, 2022 -

Hello,

I an looking to learn more professional best practices for handling exceptions. I am learning a bit about abstracting implementation details by catching lower level exceptions and then raising new ones. I am wondering how do you know all of the exceptions a function could possibly raise and what things it relies on could raise? Right now I try and read documentation and it doesn’t always say what could be raised, I try different bad inputs, and sometimes read source code. Is there a better way or more reading on this topic you would recommend. Is it possibly for this to be automated to say what could be raised?

Exception handling in python Jan 25, 2022
r/learnpython
4y ago
Best practices for try/except blocks in Python script. Sep 20, 2024
r/learnpython
last yr.
Catch "any" exception? Jul 1, 2024
r/learnpython
2y ago
More results from reddit.com
🌐
Real Python
realpython.com › ref › best-practices › exception-handling
exception handling | Python Best Practices – Real Python
This practice keeps failures closer to their root causes. Raise low, catch high. Let lower-level functions raise exceptions and catch them at the edges of your program, such as a CLI command, web handler, or GUI event loop.
Top answer
1 of 5
97

Robust exception handling (in Python) - a "best practices for Python exceptions" blog post I wrote a while ago. You may find it useful.

Some key points from the blog:

Never use exceptions for flow-control

Exceptions exist for exceptional situations: events that are not a part of normal execution.

Consider 'find' on a string returning -1 if the pattern isn't found, but indexing beyond the end of a string raises an exception. Not finding the string is normal execution.

Handle exceptions at the level that knows how to handle them

...

The best place is that piece of code that can handle the exception. For some exceptions, like programming errors (e.g. IndexError, TypeError, NameError etc.) exceptions are best left to the programmer / user, because "handling" them will just hide real bugs.

Always ask "is this the right place to handle this exception?" and be careful with catching all exceptions.

Document the exceptions thrown by your code

...

thinking about which exceptions your code may throw will help you write better, safer and more encapsulated code

2 of 5
38

I read several times in books that exceptions should never ever hold a string, because strings themselves can throw exceptions. Any real truth to this?

What?

Please provide a reference or a link to this. It's totally untrue.

Since all objects can throw exceptions, no object could be contained in an exception by that logic.

No, the "no strings" is simply crazy in a Python context. Perhaps you read it in a C++ context.


Edit

Once upon a time (back in the olden days) you could raise a Python exception by name instead of by the actual class.

raise "SomeNameOfAnExceptionClass"

This is bad. But this is not including a string inside an exception. This is naming the exception with a string instead of the actual class object. In 2.5, this can still work, but gets a deprecation warning.

Perhaps this is what you read "Do not raise an exception with a string name"

🌐
GeeksforGeeks
geeksforgeeks.org › python › python-exception-handling
Python Exception Handling - GeeksforGeeks
Explanation: try block attempts ... for some common exceptions. We can handle errors more efficiently by specifying the types of exceptions we expect....
Published: May 29, 2026
🌐
Medium
medium.com › delivus › exception-handling-best-practices-in-python-a-fastapi-perspective-98ede2256870
Exception Handling Best Practices in Python: A FastAPI Perspective | by Hyunil Kim | 딜리버스 | Medium
December 15, 2024 - Handle exceptions at the right level: Deal with exceptions at the level where you have enough context to handle them properly. Don’t suppress exceptions: Log them appropriately and provide meaningful feedback.
Find elsewhere
🌐
Mimo
mimo.org › glossary › python › error-handling
Python Error Handling: Syntax, Techniques, and Best Practices
Master Python error handling with try, except, finally, and custom exceptions. Catch, raise, and log errors to build more reliable programs.
🌐
Jerry Ng
jerrynsh.com › python-exception-handling-patterns-and-best-practices
Python Exception Handling: Patterns and Best Practices
July 7, 2026 - In terms of best practices – it is generally recommended to use the from e syntax when raising a new exception from an inner except block. This allows us to preserve the stack trace of the original error, which (again) can be helpful for debugging.
🌐
Pybites
pybit.es › articles › python-errors-should-not-pass-silently
Avoiding Silent Failures in Python: Best Practices for Error Handling – Pybites
August 7, 2023 - It’s advisable to handle the error, any error. At the very least, log it. Overall, in software it’s usually better to fail fast than to hide an error which causes a bug down the line that now is further away from the original source / cause. Note that if you really want to mute an error (after all, there is an “Unless explicitly silenced” part in the Zen of Python), you can do that explicitly with the suppress context manager from the contextlib module:
🌐
Medium
harithj.medium.com › mastering-error-handling-in-python-a-comprehensive-guide-b0202840a5ad
Mastering Error Handling in Python: A Comprehensive Guide | by Harith Javed Bakhrani | Medium
April 27, 2023 - Writing code that can anticipate and gracefully handle errors not only improves the stability of your applications but also makes them easier to maintain and debug. In this comprehensive guide, we’ll explore best practices for error handling in Python, covering everything from custom exceptions to the proper use of try-except blocks.
🌐
pythoncodelab
pythoncodelab.com › home › python: error handling best practices
Python: Error Handling Best Practices
November 9, 2024 - When handling an error in the code we should only catch the exceptions that are anticipated. If you catch a generic exception like “Exception” it may mask the root cause of an error making it difficult for us to identify the root cause of ...
🌐
Metana
metana.io › metana: coding bootcamp | software, web3 & cyber › full-stack › mastering python exception handling: try-except best practices & examples
Mastering Python Exception Handling: Try-Except Best Practices & Examples (2025)
December 1, 2025 - It is Python’s mechanism for handling exceptions and making sure your application can gracefully recover from unexpected errors. This article explores the best practices for Python try except, focusing on its implementation, common mistakes to avoid, and how to use it effectively.
🌐
LinkedIn
linkedin.com › advice › 3 › what-best-practices-error-handling-python-zlpde
Best Practices for Python Error Handling
1 billion members | Manage your professional identity. Build and engage with your professional network. Access knowledge, insights and opportunities.
🌐
Earthly
earthly.dev › blog › error-handling-in-python
Error Handling in Python - Earthly Blog
July 19, 2023 - We’ve also discussed best practices like writing clear error messages, testing, consistency, documentation, and logging. After reading, you should be adept at handling errors in Python to create more reliable code.
🌐
Analytics Vidhya
analyticsvidhya.com › home › python exception handling: best practices for error-free code
Python Exception Handling: Best Practices for Error-Free Code
April 9, 2024 - Here’s why it’s crucial:Error Identification · Why it matters: Exception handling allows you to identify errors quickly and accurately. Best practice: Implement precise error messages to facilitate troubleshooting.
🌐
Qiita
qiita.com › python
Pythonで例外を投げるときのベストプラクティス #Python - Qiita
May 5, 2018 - ライブラリ開発やデータ分析ツールの作成の際に適切に例外処理を行うことで、頑健かつバグの発見をしやすいシステムを作れるようになる。Pythonは他言語と比べて例外処理のオーバーヘッドが軽いので積極的に利用することで、高速かつ安全なコードを書くことができます。
🌐
Amazon Japan
amazon.co.jp › Pythonのエラーハンドリングのベストプラクティスは何ですか?(Pythonのお悩み解決のヒント)-尺一麟-ebook › dp › B0CBF76YB1
Amazon
Amazon.co.jp: Pythonのエラーハンドリングのベストプラクティスは何ですか?(Pythonのお悩み解決のヒント) eBook : 尺一麟: Kindle Store
🌐
LinkedIn
linkedin.com › pulse › error-handling-python-manish-v-
Mastering Error Handling in Python: From Basics to Best Practices"
January 20, 2024 - By keeping track of errors, warnings, and other essential application messages, logging helps developers maintain, debug, and improve their Python applications more effectively. Error handling isn't just a theoretical concept; it's rooted deeply in real-world applications. Understanding how to predict and address potential errors in practical scenarios ensures that your Python applications are not only robust but also user-friendly.
🌐
Vegibit
vegibit.com › python-error-handling-best-practices
Python Error Handling Best Practices
A practical corner for shipping real web stuff: layouts that behave, APIs that don’t bite, and tiny “why is this broken” moments turned into repeatable fixes.