- Python's standard library is known for all sorts of handy raise, not to mention a variety of third-party libraries of varying quality?
- The official PEP8 standard of Python clearly requires
When
catching exceptions, mention specific exception
This is clearly the general rule of the Overlord terms, if you want customers to catch specific exception, then at least your language level to have raise specific exception mechanism (similar to Java? ), but Python does not.
It's really a bit annoying.
Reply content:
You guys, don't want to get a big story. Before you take Java to Black Python, don't forget that there are unchecked exception in your home language.
This is clearly the original:
When
catching exceptions, mention specific exceptions
whenever possible instead of using
a bare exc Ept:clause.
The meaning is too clear:
- If possible, try to capture more specific exception.
- Instead of using except: To catch all exceptions.
You have to understand that PEP8 is not a mandatory standard. Even if it is mandatory, except Exception: enough to meet this clause, do not know the main "laugh and cry" in crying what.
The reasons need to be combined with PEP8 's section on exception:
Derive exceptions from Exception rather than baseexception
The most common of the exception inherited from Baseexception is keyboardinterrupt. If your code uses except: To catch an exception, the long-executed code cannot be terminated with CTRL + C.
A well-coded library should define itself as a base exception class (for example, fooexception), Then inherit more detailed exception (such as Illigalfooexception) from this exception. This way your code can except Fooexception capture all possible exceptions to the library, or you can use except illigalfooexception to catch more detailed exceptions.
If you are using a library that does not define an exception in this form, it will reuse 6 directly. Built-in Exceptions
, it is recommended to throw it away directly. The title of the Lord should hope that Python has Java-like checked exception, that is, the function body can be thrown out of the exception type, or even do a compile check, rather than as now, can only look at the document and the source code.
But Python is a dynamic language that is not much checked at compile time, not only do not tell you can throw what exception, and even the parameters and return value can be what type is not, why alone tugging not exception specification say? The style of Python is to decide when everything is running and not to use it. Let me give you a handy example of raise, @yegle replied,
excited!
Just last week to reconstruct a small program, search some exception processing related data, list:
0. Robust exception Handling
Well. This article blog post first mentions the
LbylAnd
EafpThese two error handling modes also refer to the choice between Exceptions vs. Error status codes.
1. Write Cleaner python:use Exceptions
This blog post is about
how to write clearer python code by using the Python exception mechanism reasonably。
This blog post also mentions that in Python, exceptions are everywhere, so don't feel raise around. After all, even if there is an unhandled exception, the program is not as direct terminated as in C + +.
2. Re-raising Exceptions
This article mentions the Try...except:/try...except Exception for the purpose of raise: not bullying:
try: ...except:# except Exception: ... raise# excited!
Look at the excellent library, werkzeug/exceptions.py at Master Mitsuhiko/werkzeug GitHub
It's all self-conscious