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.
Answer from Eli Bendersky on Stack OverflowDocument the exceptions thrown by your code
...
thinking about which exceptions your code may throw will help you write better, safer and more encapsulated code
Best Practices for Python Exceptions? - Stack Overflow
Confused about the best practices for error handling
Python errors as values: Comparing useful patterns from Rust and Go
Would you use Go / Rust style error handling in Python?
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?
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
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"