Python development [Chapter 7]: Python exception handling and python exception handling

Source: Internet
Author: User

Python development [Chapter 7]: Python exception handling and python exception handling

I. Exception Handling

1. exception basis

In the programming process, in order to increase friendliness, when a program encounters a bug, the error information is generally not displayed to the user, but a prompt page. In other words, the user is not allowed to see the yellow pages !!!

# Exception Handling list = ["hello", "world"] try: list [3] handle T IndexError as e: print ("IndexError", e) # IndexError list index out of range

The above program indicates that if the code in try has an IndexError error, execute the code below the retry T, and do not display the error information to the user, and the program will not stop; currently, you can only handle the IndexError exception. To handle multiple exceptions, you can write as follows:

# Multiple Exception Handling list = ["hello", "world"] try: list [3] handle T IndexError as e: print ("IndexError", e) handle T KeyError as e: print ("KeyError", e) failed t ValueError as e: print ("ValueError", e) # IndexError list index out of range

10 thousand of python exceptions can be abnormal: Exception, which can catch any exceptions.

# Universal Exception Handling list = ["hello", "world"] try: list [3] failed t Exception as e: print ("Error", e) # Error list index out of range
AttributeError attempts to access a tree without an object, such as foo. x, but foo has no attribute xIOError input/output exception. Basically, the file ImportError cannot be opened and modules or packages cannot be introduced; basically, it is a path problem or name error. IndentationError syntax error (subclass). The Code does not correctly align the IndexError subscript index beyond the sequence boundary. For example, if x has only three elements, however, I tried to access x [5] KeyError. I tried to access the KeyboardInterrupt Ctrl + C key that does not exist in the dictionary. I was pressed NameError and used the SyntaxError Python code, which is invalid, code cannot be compiled (I personally think this is a syntax error and I wrote an error) TypeError passed in the object type and the required non-conforming UnboundLocalError attempts to access a local variable that has not been set yet, basically, because there is another global variable with the same name, you think that you are accessing its ValueError and passing in a value that the caller does not expect, even if the value type is correctFrequently Used exceptionsMore exceptions

 

2. Other abnormal Structures

# Other abnormal structures try: # main code block passexcept KeyError as e: # When an exception occurs, execute this block passelse: # after the main code block is executed, execute this block passfinally: # whether it is abnormal or not, the block pass is finally executed.

 

3. Custom exceptions 

Actively trigger exceptions

# Actively trigger the Exception try: raise Exception ('error... ') Failed t Exception as e: print (e) # error...
Custom exception
# Custom Exception class Diyexception (Exception): def _ init _ (self, msg): self. message = msg def _ str _ (self): return self. messageerror = Diyexception ("error .... ") try: raise errorexcept Diyexception as e: print (e) # error ....

  

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.