Python learning-Exception Handling and python exception handling

Source: Internet
Author: User

Python learning-Exception Handling and python exception handling

1. Exception basics:

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 rather a page with a realistic prompt.

Basic Syntax:

1 try:2     pass3 except Exception as e:4 5     print(e)

Instance: the sum of two numbers

1 while True: 2 num1 = input ('num1: ') 3 num2 = input ('num2:') 4 try: 5 num1 = int (num1) 6 num2 = int (num2) 7 result = num1 + num2 8 blocks t Exception as e: 9 print ('exception occurred, Info: ') 10 print (e)


2. Exception types

There are many types of exceptions in python. Each exception is specially used to handle an exception !!!

Common exceptions:

1 AttributeError attempts to access a property that does not exist in an object, such as foo. x, but foo has no attribute x 2 IOError input/output exception. Basically, file 3 ImportError cannot be opened and modules or packages cannot be introduced; basically it is a path problem or name error 4 IndentationError syntax error (subclass); the Code is not correctly aligned with the 5 IndexError subscript index beyond the sequence boundary. For example, when x has only three elements, however, I tried to access x [5] 6 KeyError. I tried to access the key that does not exist in the dictionary. 7 KeyboardInterrupt Ctrl + C was pressed 8 NameError. The Python code of variable 9 SyntaxError, which is not assigned to the object, is invalid, code cannot be compiled (I personally think this is a syntax error and I wrote it wrong) 10 TypeError passed in object type and the requirement does not meet 11 UnboundLocalError attempts to access a local variable that has not been set yet, basically, because there is another global variable with the same name, 12 leads you to think that you are accessing it 13 ValueError and passing in a value that the caller does not expect, even if the value type is correct

Example 1: IndexError

1 dic = ["haha", 'hehe']2 try:3     dic[10]4 except IndexError as e:5     print(e)     # list index out of range

Example 2: ValueError

1 s1 = 'hello'2 try:3     int(s1)4 except ValueError as e:5     print(e)   # invalid literal for int() with base 10: 'hello'

For the above instance, the exception class can only be used to handle the specified exception. If the exception is not specified, it cannot be processed.

1 s1 = 'hello' 2 try: 3 int (s1) 4 handle T IndexError as e: 5 print (e) # If no exception is caught, the program reports an error directly.

In this case, consider any exceptions that may occur in the program code.

1 s1 = 'hello'2 try:3     int(s1)4 except IndexError as e:5     print(e)6 except KeyError as e:7     print(e)8 except ValueError as e:9     print(e)

However, in python exceptions, there are 10 thousand exceptions: Exception, which can catch any exceptions, that is:

1 s1 = 'hello'2 try:3     int(s1)4 except Exception as e:5     print(e)

This omnipotent Exception exists, but other exceptions cannot be ignored. Generally, for exceptions with special processing or reminders, you must first define the Exception to ensure the normal operation of the program.

1 s1 = 'hello' 2 try: 3 int (s1) 4 random t KeyError as e: 5 print ('key error') 6 random t IndexError as e: 7 print ('index error') 8 TB t Exception, as e: 9 print ('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.