程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Advanced Series (11)

編輯:Python

abnormal

Exception handling is an art , Once you master , Will give you infinite power . I'm going to show you some ways we can handle exceptions .

In the most basic terms, we know try/except clause . The code that may trigger the exception will be put into try In the sentence block , The exception handling code will be in except Implement... In a statement block . This is a simple example :

try:
    file = open('test.txt', 'rb')
except IOError as e:
    print('An IOError occurred. {}'.format(e.args[-1]))

In the example above , We're just dealing with one IOError It's abnormal . What most beginners don't know is , We can handle multiple exceptions .

Handle multiple exceptions

There are three ways to handle multiple exceptions .

The first method needs to put all possible exceptions into a tuple . like this :

try:
    file = open('test.txt', 'rb')
except (IOError, EOFError) as e:
print("An error occurred. {}".format(e.args[-1]))

Another way is to separate each exception in a separate except Processing in statement block . How many... Do we want except All sentence blocks can be . Here is an example :

try:
    file = open('test.txt', 'rb')
except EOFError as e:
    print("An EOF error occurred.")
    raise e
except IOError as e:
    print("An error occurred.")
    raise e

In the above way , If the exception is not by the first except Statement block processing , Then it may be processed by the next statement block , Or it won't be dealt with at all .

Now? , The last method catches all exceptions :

try:
    file = open('test.txt', 'rb')
except Exception:
    # Print some exception logs , If you want
    raise

When you don't know what kind of exception your program will throw , The above method can be very helpful .

finally clause

We wrapped our main program code in try clause . Then we wrap some code into a except clause , It will be try Execute when the code in the clause triggers an exception .

In the following example , We will also use the third clause , That's it finally clause . Package to finally The code in the exception will be triggered whether it is executed or not . This can be used to clean up after script execution .

Here is a simple example :

try:
    file = open('test.txt', 'rb')
except IOError as e:
    print('An IOError occurred. {}'.format(e.args[-1]))
finally:
    print("This would be printed whether or not an exception occurred!"

Output: An IOError occurred. No such file or directory

 This would be printed whether or not an exception occurred!

try/else clause

We often want to execute some code without triggering an exception . This can be easily done through a else To achieve .

Someone may have asked : If you just want some code to execute without triggering an exception , Why don't you just put the code in try Inside? ?

The answer is , In that case, any exception in this code will still be try Capture , And you don't necessarily want that .

Most people don't use else clause , And frankly, I don't use it on a large scale . Here is an example :

try:
    print('I am sure no exception is going to occur!')
except Exception:
    print('exception')
else:
    print('This would only run if no exception occurs.')
finally:
    print('This would be printed in every case.')

  The code here will only be in try Run when no exception is triggered in the statement ,

   But the exception here will * Can't * Captured

Output: I am sure no exception is going to occur!

This would only run if no exception occurs.

This would be printed in every case.

else Clauses are only executed without exceptions , And it will be in finally Execute before statement .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved