>>1/0Traceback(mostrecentcalllast):File"",line1,in1/0ZeroDivisionError:inte" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python基礎學習筆記(九)

系統 1845 0

python基礎學習筆記(九)

2013-04-28 01:15 ?蟲師 閱讀( ... ) 評論( ... ) 編輯 收藏

?

?

python 異常

?

python 用異常對象( exception?object )來表示異常情況。遇到錯誤后,會引發異常。如果異常對象并未被處理或捕捉,程序就會用所謂的?回溯( Traceback ,?一種錯誤信息)終止執行:

                >>> 1/
                
                  0

Traceback (most recent call last):
  File 
                
                
                  "
                
                
                  
                
                
                  "
                
                , line 1, 
                
                  in
                
                
                  
    1/
                  
                    0
ZeroDivisionError: integer division 
                  
                  
                    or
                  
                   modulo by zero
                
              

?

?

raise? 語句

為了引發異常,可以使用一個類( Exception 的子類)或者實例參數數調用 raise? 語句。下面的例子使用內建的 Exception 異常類:

                >>> 
                
                  raise
                
                 Exception    
                
                  #
                
                
                  引發一個沒有任何錯誤信息的普通異常
                
                
                  
Traceback (most recent call last):
  File 
                
                
                  "
                
                
                  
                
                
                  "
                
                , line 1, 
                
                  in
                
                
                  
                    raise
                  
                  
                     Exception
Exception

                  
                  >>> 
                  
                    raise
                  
                   Exception(
                  
                    '
                  
                  
                    hyperdrive overload
                  
                  
                    '
                  
                  )   
                  
                    #
                  
                  
                     添加了一些異常錯誤信息
                  
                  
                    
Traceback (most recent call last):
  File 
                  
                  
                    "
                  
                  
                    
                  
                  
                    "
                  
                  , line 1, 
                  
                    in
                  
                  
                    
                      raise
                    
                     Exception(
                    
                      '
                    
                    
                      hyperdrive overload
                    
                    
                      '
                    
                    
                      )
Exception: hyperdrive overload
                    
                  
                
              

?

系統自帶的內建異常類:

                >>> 
                
                  import
                
                
                   exceptions

                
                >>>
                
                   dir(exceptions)
[
                
                
                  '
                
                
                  ArithmeticError
                
                
                  '
                
                , 
                
                  '
                
                
                  AssertionError
                
                
                  '
                
                , 
                
                  '
                
                
                  AttributeError
                
                
                  '
                
                , 
                
                  '
                
                
                  BaseException
                
                
                  '
                
                , 
                
                  '
                
                
                  BufferError
                
                
                  '
                
                , 
                
                  '
                
                
                  BytesWarning
                
                
                  '
                
                , 
                
                  '
                
                
                  DeprecationWarning
                
                
                  '
                
                , 
                
                  '
                
                
                  EOFError
                
                
                  '
                
                , 
                
                  '
                
                
                  EnvironmentError
                
                
                  '
                
                , 
                
                  '
                
                
                  Exception
                
                
                  '
                
                , 
                
                  '
                
                
                  FloatingPointError
                
                
                  '
                
                , 
                
                  '
                
                
                  FutureWarning
                
                
                  '
                
                , 
                
                  '
                
                
                  GeneratorExit
                
                
                  '
                
                , 
                
                  '
                
                
                  IOError
                
                
                  '
                
                , 
                
                  '
                
                
                  ImportError
                
                
                  '
                
                , 
                
                  '
                
                
                  ImportWarning
                
                
                  '
                
                , 
                
                  '
                
                
                  IndentationError
                
                
                  '
                
                , 
                
                  '
                
                
                  IndexError
                
                
                  '
                
                , 
                
                  '
                
                
                  KeyError
                
                
                  '
                
                , 
                
                  '
                
                
                  KeyboardInterrupt
                
                
                  '
                
                , 
                
                  '
                
                
                  LookupError
                
                
                  '
                
                , 
                
                  '
                
                
                  MemoryError
                
                
                  '
                
                , 
                
                  '
                
                
                  NameError
                
                
                  '
                
                , 
                
                  '
                
                
                  NotImplementedError
                
                
                  '
                
                , 
                
                  '
                
                
                  OSError
                
                
                  '
                
                , 
                
                  '
                
                
                  OverflowError
                
                
                  '
                
                , 
                
                  '
                
                
                  PendingDeprecationWarning
                
                
                  '
                
                , 
                
                  '
                
                
                  ReferenceError
                
                
                  '
                
                , 
                
                  '
                
                
                  RuntimeError
                
                
                  '
                
                , 
                
                  '
                
                
                  RuntimeWarning
                
                
                  '
                
                , 
                
                  '
                
                
                  StandardError
                
                
                  '
                
                , 
                
                  '
                
                
                  StopIteration
                
                
                  '
                
                , 
                
                  '
                
                
                  SyntaxError
                
                
                  '
                
                , 
                
                  '
                
                
                  SyntaxWarning
                
                
                  '
                
                , 
                
                  '
                
                
                  SystemError
                
                
                  '
                
                , 
                
                  '
                
                
                  SystemExit
                
                
                  '
                
                , 
                
                  '
                
                
                  TabError
                
                
                  '
                
                , 
                
                  '
                
                
                  TypeError
                
                
                  '
                
                , 
                
                  '
                
                
                  UnboundLocalError
                
                
                  '
                
                , 
                
                  '
                
                
                  UnicodeDecodeError
                
                
                  '
                
                , 
                
                  '
                
                
                  UnicodeEncodeError
                
                
                  '
                
                , 
                
                  '
                
                
                  UnicodeError
                
                
                  '
                
                , 
                
                  '
                
                
                  UnicodeTranslateError
                
                
                  '
                
                , 
                
                  '
                
                
                  UnicodeWarning
                
                
                  '
                
                , 
                
                  '
                
                
                  UserWarning
                
                
                  '
                
                , 
                
                  '
                
                
                  ValueError
                
                
                  '
                
                , 
                
                  '
                
                
                  Warning
                
                
                  '
                
                , 
                
                  '
                
                
                  WindowsError
                
                
                  '
                
                , 
                
                  '
                
                
                  ZeroDivisionError
                
                
                  '
                
                , 
                
                  '
                
                
                  __doc__
                
                
                  '
                
                , 
                
                  '
                
                
                  __name__
                
                
                  '
                
                , 
                
                  '
                
                
                  __package__
                
                
                  '
                
                ]
              

哇!好多,常用的內建異常類:

python基礎學習筆記(九)_第1張圖片

?

?

自定義異常

盡管內建的異常類已經包括了大部分的情況,而且對于很多要求都已經足夠了,但有些時候還是需要創建自己的異常類。

和常見其它類一樣 ---- 只是要確保從 Exception 類繼承,不管直接繼承還是間接繼承。像下面這樣:

                >>> 
                
                  class
                
                 someCustomExcetion(Exception):
                
                  pass
                
              

當然,也可以為這個類添加一些方法。

?

?

捕捉異常

我們可以使用? try/except? 來實現異常的捕捉處理。

假設創建了一個讓用戶輸入兩個數,然后進行相除的程序:

                x = input(
                
                  '
                
                
                  Enter the first number: 
                
                
                  '
                
                
                  )
y 
                
                = input(
                
                  '
                
                
                  Enter the second number: 
                
                
                  '
                
                
                  )

                
                
                  print
                
                 x/
                
                  y


                
                
                  #
                
                
                  運行并且輸入
                
                
Enter the first number: 10
                
                  
Enter the second number: 0

Traceback (most recent call last):
  File 
                
                
                  "
                
                
                  I:/Python27/yichang
                
                
                  "
                
                , line 3, 
                
                  in
                
                
                  
                    print
                  
                   x/
                  
                    y
ZeroDivisionError: integer division 
                  
                  
                    or
                  
                   modulo by zero
                
              

?

為了捕捉異常并做出一些錯誤處理,可以這樣寫:

                
                  try
                
                
                  :
    x 
                
                = input(
                
                  '
                
                
                  Enter the first number: 
                
                
                  '
                
                
                  )
    y 
                
                = input(
                
                  '
                
                
                  Enter the second number: 
                
                
                  '
                
                
                  )
    
                
                
                  print
                
                 x/
                
                  y

                
                
                  except
                
                
                   ZeroDivisionError:
  
                
                
                  print
                
                
                  "
                
                
                  輸入的數字不能為0!
                
                
                  "
                
                
                  #
                
                
                  再來云行
                
                
>>>
                
                   
Enter the first number: 
                
                10
                
                  
Enter the second number: 0
輸入的數字不能為0!           
                
                
                  #
                
                
                  怎么樣?這次已經友好的多了
                
              

假如,我們在調試的時候引發異常會好些,如果在與用戶的進行交互的過程中又是不希望用戶看到異常信息的。那如何開啟 / 關閉?“屏蔽”機制?

                
                  class
                
                
                   MuffledCalulator:
    muffled 
                
                = False   
                
                  #
                
                
                  這里默認關閉屏蔽
                
                
                  def
                
                
                   calc(self,expr):
        
                
                
                  try
                
                
                  :
            
                
                
                  return
                
                
                   eval(expr)
        
                
                
                  except
                
                
                   ZeroDivisionError:
            
                
                
                  if
                
                
                   self.muffled:
                
                
                
                  print
                
                
                  '
                
                
                  Divsion by zero is illagal
                
                
                  '
                
                
                  else
                
                
                  :
                
                
                
                  raise
                
                
                  #
                
                
                  運行程序:
                
                
>>> calculator =
                
                   MuffledCalulator()

                
                >>> calculator.calc(
                
                  '
                
                
                  10/2
                
                
                  '
                
                
                  )

                
                5
>>> calculator.clac(
                
                  '
                
                
                  10/0
                
                
                  '
                
                
                  )

Traceback (most recent call last):
  File 
                
                
                  "
                
                
                  
                
                
                  "
                
                , line 1, 
                
                  in
                
                
                  
                    
    calculator.clac(
                  
                  
                    '
                  
                  
                    10/0
                  
                  
                    '
                  
                  
                    )
AttributeError: MuffledCalulator instance has no attribute 
                  
                  
                    '
                  
                  
                    clac
                  
                  
                    '
                  
                  
                    #
                  
                  
                    異常信息被輸出了
                  
                  

>>> calculator.muffled = True   
                  
                    #
                  
                  
                    現在打開屏蔽
                  
                  
>>> calculator.calc(
                  
                    '
                  
                  
                    10/0
                  
                  
                    '
                  
                  
                    )
Divsion by zero 
                  
                  
                    is
                  
                   illagal 
                
              

?

?

多個 except? 子句

如果運行上面的(輸入兩個數,求除法)程序,輸入面的內容,就會產生另外一個異常:

                
                  try
                
                
                  :
    x 
                
                = input(
                
                  '
                
                
                  Enter the first number: 
                
                
                  '
                
                
                  )
    y 
                
                = input(
                
                  '
                
                
                  Enter the second number: 
                
                
                  '
                
                
                  )
    
                
                
                  print
                
                 x/
                
                  y

                
                
                  except
                
                
                   ZeroDivisionError:
  
                
                
                  print
                
                
                  "
                
                
                  輸入的數字不能為0!
                
                
                  "
                
                
                  #
                
                
                  運行輸入:
                
                
>>>
                
                   
Enter the first number: 
                
                10
                
                  
Enter the second number: 
                
                
                  '
                
                
                  hello.word
                
                
                  '
                
                
                  #
                
                
                  輸入非數字
                
                
                  
Traceback (most recent call last):
  File 
                
                
                  "
                
                
                  I:\Python27\yichang
                
                
                  "
                
                , line 4, 
                
                  in
                
                
                  
                    print
                  
                   x/
                  
                    y
TypeError: unsupported operand type(s) 
                  
                  
                    for
                  
                   /: 
                  
                    '
                  
                  
                    int
                  
                  
                    '
                  
                  
                    and
                  
                  
                    '
                  
                  
                    str
                  
                  
                    '
                  
                  
                    #
                  
                  
                    又報出了別的異常信息
                  
                
              

?

好吧!我們可以再加個異常的處理來處理這種情況:

                
                  try
                
                
                  :
    x 
                
                = input(
                
                  '
                
                
                  Enter the first number: 
                
                
                  '
                
                
                  )
    y 
                
                = input(
                
                  '
                
                
                  Enter the second number: 
                
                
                  '
                
                
                  )
    
                
                
                  print
                
                 x/
                
                  y

                
                
                  except
                
                
                   ZeroDivisionError:
    
                
                
                  print
                
                
                  "
                
                
                  輸入的數字不能為0!
                
                
                  "
                
                
                  except
                
                 TypeError:           
                
                  #
                
                
                   對字符的異常處理
                
                
                  print
                
                
                  "
                
                
                  請輸入數字!
                
                
                  "
                
                
                  #
                
                
                  再來運行:
                
                
>>>
                
                   
Enter the first number: 
                
                10
                
                  
Enter the second number: 
                
                
                  '
                
                
                  hello,word
                
                
                  '
                
                
                  
請輸入數字!
                
              

?

?

一個塊捕捉多個異常

我們當然也可以用一個塊來捕捉多個異常:

                
                  try
                
                
                  :
    x 
                
                = input(
                
                  '
                
                
                  Enter the first number: 
                
                
                  '
                
                
                  )
    y 
                
                = input(
                
                  '
                
                
                  Enter the second number: 
                
                
                  '
                
                
                  )
    
                
                
                  print
                
                 x/
                
                  y

                
                
                  except
                
                
                   (ZeroDivisionError,TypeError,NameError):
    
                
                
                  print
                
                
                  "
                
                
                  你的數字不對!
                
                
                  "
                
              

?

?

捕捉全部異常

就算程序處理了好幾種異常,比如上面的程序,運行之后,假如我輸入了下面的內容呢

                >>>
                
                   
Enter the first number: 
                
                10
                
                  
Enter the second number:   
                
                
                  #
                
                
                  不輸入任何內容,回車
                
                
                  
Traceback (most recent call last):
  File 
                
                
                  "
                
                
                  I:\Python27\yichang
                
                
                  "
                
                , line 3, 
                
                  in
                
                
                  
                    
    y 
                  
                  = input(
                  
                    '
                  
                  
                    Enter the second number: 
                  
                  
                    '
                  
                  
                    )
  File 
                  
                  
                    "
                  
                  
                    
                  
                  
                    "
                  
                  
                    , line 0
    
   
                  
                  ^
                  
                    
SyntaxError: unexpected EOF 
                  
                  
                    while
                  
                   parsing
                
              

?

暈死 ~ !?怎么辦呢?總有被我們不小心忽略處理的情況,如果真想用一段代碼捕捉所有異常,那么可在 except 子句中忽略所有的異常類:

                
                  try
                
                
                  :
    x 
                
                = input(
                
                  '
                
                
                  Enter the first number: 
                
                
                  '
                
                
                  )
    y 
                
                = input(
                
                  '
                
                
                  Enter the second number: 
                
                
                  '
                
                
                  )
    
                
                
                  print
                
                 x/
                
                  y

                
                
                  except
                
                
                  :
    
                
                
                  print
                
                
                  '
                
                
                  有錯誤發生了!
                
                
                  '
                
                
                  #
                
                
                  再來輸入一些內容看看
                
                
>>>
                
                   
Enter the first number: 
                
                
                  '
                
                
                  hello
                
                
                  '
                
                 *
                
                   )0 
有錯誤發生了!
                
              

?

?

結束

別急!再來說說最后一個情況,好吧,用戶不小心輸入了錯誤的信息,能不能再給次機會輸入?我們可以加個循環,保你輸對時才結束:

                
                  while
                
                
                   True:
    
    
                
                
                  try
                
                
                  :
        x 
                
                = input(
                
                  '
                
                
                  Enter the first number: 
                
                
                  '
                
                
                  )
        y 
                
                = input(
                
                  '
                
                
                  Enter the second number: 
                
                
                  '
                
                
                  )
        value 
                
                = x/
                
                  y
        
                
                
                  print
                
                
                  '
                
                
                  x/y is
                
                
                  '
                
                
                  ,value
                  
break
except : print ' 列效輸入,再來一次! ' # 運行 >>> Enter the first number: 10 Enter the second number: 列效輸入,再來一次! Enter the first number: 10 Enter the second number: ' hello ' 列效輸入,再來一次! Enter the first number: 10 Enter the second number: 2 x /y is 5

?

?------------------------

溫馨提示:因為是學習筆記,盡量精簡了文字,所以,你要跟著做才能體會,光看是沒用的(也沒意思)。

?

?


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 石景山区| 南皮县| 克什克腾旗| 彭山县| 南昌市| 南部县| 肇州县| 沅江市| 金沙县| 灵宝市| 左贡县| 宝丰县| 滁州市| 陈巴尔虎旗| 芦山县| 农安县| 美姑县| 河南省| 邻水| 盐池县| 玛沁县| 辛集市| 老河口市| 乌鲁木齐市| 辽中县| 永嘉县| 武威市| 基隆市| 梓潼县| 营山县| 安庆市| 湄潭县| 高唐县| 昭平县| 濮阳县| 绥滨县| 潮安县| 临沂市| 科技| 清水县| 五河县|