python-異常處理

來源:互聯網
上載者:User

標籤:404錯誤   斷言   資訊   無法   while   全域   地方   容器   路徑   

一、異常

異常是什嗎?

       就是影響程式正常執行的事件,異常在程式執行的過程中發生,指令碼發生異常時,我們需要捕獲它,否則程式就會終止。

異常處理:

       1、捕獲異常:try:。。。except 。。。。。

 1 while True: 2     try: 3         num1=input(‘>>:‘) 4         num2 = input(‘>>:‘) 5         num1=int(num1) #   出錯,不再往下執行try裡面的代碼塊, 直接進入except 6         num2 = int(num2) 7         num=num1+num2 8         print(num) 9     except Exception as e:10         print(e)  #捕獲錯誤資訊   程式重新執行

       2、複雜的異常處理結構:

             try:

                  代碼塊1

             except:

                  代碼塊2

             else:

                  代碼塊3

             finally:

                  代碼塊4

            不出異常:1-3-4

               出異常:1-2-4

       3、異常種類

           Exception可以捕獲所有的異常

           常用的其他異常

 1 AttributeError 試圖訪問一個對象沒有的樹形,比如foo.x,但是foo沒有屬性x 2 IOError 輸入/輸出異常;基本上是無法開啟檔案 3 ImportError 無法引入模組或包;基本上是路徑問題或名稱錯誤 4 IndentationError 語法錯誤(的子類) ;代碼沒有正確對齊 5 IndexError 下標索引超出序列邊界,比如當x只有三個元素,卻試圖訪問x[5] 6 KeyError 試圖訪問字典裡不存在的鍵 7 KeyboardInterrupt Ctrl+C被按下 8 NameError 使用一個還未被賦予對象的變數 9 SyntaxError Python代碼非法,代碼不能編譯(個人認為這是語法錯誤,寫錯了)10 TypeError 傳入物件類型與要求的不符合11 UnboundLocalError 試圖訪問一個還未被設定的局部變數,基本上是由於另有一個同名的全域變數,12 導致你以為正在訪問它13 ValueError 傳入一個調用者不期望的值,即使值的類型是正確的

         對於特殊處理或提醒的異常需要先定義,最後定義Exception來確保程式正常運行

 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 e10 except Exception as e:11     print e

          主動觸發異常

          raise ..............

 1 while True: 2     try: 3         num1=input(‘>>:‘) 4         num2 = input(‘>>:‘) 5         num1=int(num1)  6         num2 = int(num2) 7         num=num1+num2 8         print(num) 9         raise Exception(‘eeeeeeeeeerrrrrrrrrrrrrrSSSSSSSS‘) #主動觸發異常10     except Exception as e:11         print(e)  #  列印出 eeeeeeeeeerrrrrrrrrrrrrrSSSSSSSS

 

斷言:  assert 條件

1 print(1)2 assert 1==13 print(2)4 ‘‘‘輸出:15          26 ‘‘‘

 

二、反射

1、允許使用者輸入 不同的url:
account/login
account/logout
home/index
home/order
2、看到不同的結果
3、輸入不存在的url,曝404錯誤

url列表=[account/login,account/logout,home/index,home/order]
以上url的函數均儲存在controller下的account指令碼中
‘‘‘
inp=input(‘請輸入url‘)
m,n=inp.split(‘/‘) #m=account n=login
去某個模組中找函數,如果函數存在就執行函數,如果不存在函數就報錯
getattr() #專門用來去某個地方擷取內部的東西 (以字串的形式擷取)
 1 from controller import account   #以上url的函數均儲存在controller下的account指令碼中 2  3 h=hasattr(account,‘login1‘) #去account模組(指令碼)中,是否存在login函數 4 print(h)    # 存在返回true    不存在返回false 5 getattr(account,‘login‘)# 去account模組(指令碼)中擷取login函數 6  7 setattr(account,‘login1‘,‘ddd‘)   #去account模組(指令碼)中添加login1函數 8 h=hasattr(account,‘login1‘)   #再次判斷是否存在login1屬性 9 print(h)  #返回true10 11 delattr(account,‘login1‘)  #去account模組(指令碼)中刪除login1函數12 h=hasattr(account,‘login1‘)   #再次判斷是否存在login1屬性13 print(h)  #返回false

        hasattr(容器,‘名稱‘)    以字串的形式判斷某對象中是否含有指定的屬性

        getattr(容器,‘名稱‘)    以字串的形式去某個對象中獲得指定的屬性

        setattr(容器,‘名稱‘,值)      以字串的形式去某個對象中設定指定的屬性

        delattr(容器,‘名稱‘)     以字串的形式去某個對象中刪除指定的屬性

 

 

 

 

                  

python-異常處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.