is Python's exception mechanism and specification quite impersonal?

Source: Internet
Author: User
    1. Python's standard library is known for all sorts of handy raise, not to mention a variety of third-party libraries of varying quality?
    2. 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:
    1. If possible, try to capture more specific exception.
    2. 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
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.