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)!