Python study Notes-error handling

Source: Internet
Author: User
There are multiple methods to handle errors in the program. one is to specify the error code, and then determine whether or not the error occurred and the cause of the error based on the returned error code. There are multiple methods to handle errors in the program. one is to specify the error code, and then determine whether or not the error occurred and the cause of the error based on the returned error code.

However, in this case, the correct return values and error codes are easily mixed. Therefore, you must write a lot of code to differentiate them, which is inconvenient. In addition, once an error occurs, it must be reported at the first level. you can handle it at the first level.

A more mature approach is try... try t... finally... this set of error handling mechanisms. This mechanism does not interfere with normal return values. At the same time, you do not need to manually report data at the first level. Instead, you only need to capture and process data at the first level.


try:print open("", 'r')n = 1 / 0except ZeroDivisionError, e:print "zeroDivisionError", eexcept ValueError, e:print "ValueError", eelse:print "No Error catched"finally:print "finally"

Note the following points for error handling:

Multiple writable T can be written to capture multiple exceptions.

The exception of the parent class can capture the exception of the subclass. the caught exception will not be passed to other exceptions.

Else can be used to handle the absence of exeption.

Finally is executed regardless of whether there are errors.

Build-in Exception types

For the inheritance relationships of Python (2.x) built-in Exception, see:

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, Build-in Exception is recommended. When the Exception we need cannot be found in the Build-in Exception, we can customize the Exception.

The following syntax is used to throw a custom Exception:

raise MyException("this is my Exception")

In the test code, we can directly print the Exception in processing the exception. However, it may not be appropriate to print logs directly in the actual production code. You can use logging. exception (msg) to print errors to logs through simple configuration. For more information about how to correctly use the python built-in logging module.

The above is the python learning notes-error handling content. For more information, see PHP Chinese website ( )!

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