Python Learning notes-error handling

Source: Internet
Author: User
There are several ways to handle error handling in a program, one is to agree on a good error code, and then to determine if an error occurred, and the cause of the error, based on the error code returned.


But it's easy to mix the correct return value with the error code, and it's very inconvenient to write a lot of code to differentiate it. In addition, once an error occurs, a first-level escalation is required, knowing that one level can handle it.


The more mature practice is try...except...finally ... This set of error handling mechanisms. This mechanism does not interfere with normal return values. There is also no need for a first-level manual escalation, but only one level of capture and processing is required.


Code:

Try:print Open ("demo.py", ' r ') n = 1/0except zerodivisionerror, e:print "Zerodivisionerror", Eexcept ValueError, E:print "ValueError", Eelse:print "No Error catched" Finally:print "finally"

There are several points to note when using error handling:


Multiple except can be written to capture multiple exception


The parent class exception can capture the exception of the subclass, and the exception that have been captured will no longer be passed to the other exception.


You can use else to handle situations where there are no exeption.


Finally, it executes regardless of whether or not an error occurs.


Types of build-in exception

Python (2.x) built-in exception's inheritance relationship is shown in:

The class hierarchy for built-in exceptions is:baseexception+--systemexit+--keyboardinterrupt+--generatorexit+-- exception+--stopiteration+--standarderror| +--buffererror| +--arithmeticerror| | +--floatingpointerror| | +--overflowerror| | +--zerodivisionerror| +--assertionerror| +--attributeerror| +--environmenterror| | +--ioerror| | +--oserror| | +--windowserror (Windows) | | +--vmserror (VMS) | +--eoferror| +--importerror| +--lookuperror| | +--indexerror| | +--keyerror| +--memoryerror| +--nameerror| | +--unboundlocalerror| +--referenceerror| +--runtimeerror| | +--notimplementederror| +--syntaxerror| | +--indentationerror| | +--taberror| +--systemerror| +--typeerror| +--valueerror| +--unicodeerror| +--unicodedecodeerror| +--unicodeencodeerror| +--unicodetranslateerror+--warning+--deprecationwarning+--pendingdeprecationwarning+--RuntimeWarning+-- syntaxwarning+--userwarning+--futurewarning+--importwarning+--unicodewarning+--BytesWarning

Of course, we can also customize a class, for example:

Class MyException (standardexception):

Of course, it is recommended to use Build-in's exception. We only customize exception when we can't find the exception we need in Build-in's exception.


Throw the custom exception using the following syntax:

Raise MyException ("This is My Exception")

In the test code, we can print exception directly in the exception processing. However, it may not be appropriate to print the log directly in the actual production code. We can use Logging.exception (msg) to print errors to the log through a simple configuration. How to correctly use the Python built-in logging module, you can open a separate article introduction.

The above is the Python learning note-Error handling content, more related to Topic.alibabacloud.com (www.php.cn)!

  • 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.