Python Review and Collation 8: Errors and exceptions

Source: Internet
Author: User

0. Description


If you want to write the user experience high code, then you need to take into account in the execution of their own written code in the process of interacting with the user may occur, that is, the need to deal with the possible exceptions, only do the work to write the user experience good code.



1. What is an exception


    • Error

The error is on the syntax (which causes the interpreter to be unexplained) or logic (that is, the code quality problem), in Python, when an error is detected, the interpreter indicates that the current stream cannot continue, and an exception occurs.

    • Abnormal

An error occurred in the program and the behavior taken outside of the normal control flow.

As explained above, it can be understood that an exception is triggered whenever the interpreter detects that an error occurred while the program was running, which is incompatible with the Python interpreter.



Exceptions in 2.Python


As follows:

exception type Simple example
nameerror Try to access an undeclared variable, or a variable that does not exist in the namespace
>>>  xpleaftraceback  (most recent call last):  file  "<stdin>",  line  1, in <module>NameError: name  ' xpleaf '  is not defined 
zerodivisionerror divisor is zero
>>> 1/0traceback  (most  recent call last):  file  "<stdin>", line 1, in < Module>zerodivisionerror: integer division or modulo by zero 
syntaxerror

python interpreter syntax error

Span style= "FONT-SIZE:14PX;" > (the only exception that is not at run time, which occurs at compile time, the Python interpreter cannot compile the associated script into a Python byte code)

>>> for  file " <stdin> ", line 1 
   for      ^syntaxerror: invalid syntax 
indexerror requested index out of sequence range
>>> alist = []>> > aList[0]Traceback  (most recent call last):  file  "<stdin>",  line 1, in <module>indexerror: list index out of range 
keyerror request a non-existent dictionary keyword
>>> adict = {' name ':   ' xpleaf ',  ' love ':  ' cl '}>>> adict[' Clyyh ']traceback  (most recent  Call last):  file  "<stdin>",  line 1, in <module>keyerror:   ' clyyh ' 
ioerror

input/output error

(any type of I/O error throws a IOError exception)

>>> f = open (' Xpleaf ') Traceback  (most  recent call last):  file  "<stdin>", line 1, in < module>ioerror: [errno 2] no such file or directory:  ' Xpleaf '
Attributeerror Attempting to access an unknown object property
>>> class MyClass (object): ... pass ... >>> myinst = MyClass () >>> Myinst.nametraceback (Most RE Cent call last): File "<stdin>", line 1, in <module>attributeerror: ' MyClass ' object have no attribute ' name '





This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1762213

Python Review and Collation 8: Errors and exceptions

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.