乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Python異常處理和異常類型

       LibraryPKU 2021-04-18

      python2.x捕獲異常語法:

      try:
          ...some functions...
      except Exception, e:
          print(e)

      python3.x捕獲異常語法:

      try:
          ...some functions...
      except Exception as e:
          print(e)

      注意這里 Exception, e 變成了 Exception as e

      python常見的異常類型

      1. NameError:嘗試訪問一個(gè)未申明的變量

      >>> v
      NameError: name 'v' is not defined

      2. ZeroDivisionError:除數(shù)為0

      >>> v = 1/0
      ZeroDivisionError: int division or modulo by zero

      3. SyntaxError:語法錯(cuò)誤

      int int
      SyntaxError: invalid syntax (<pyshell#14>, line 1)

      4. IndexError:索引超出范圍

      List = [2]
      >>> List[3]
      Traceback (most recent call last):
        File "<pyshell#18>", line 1, in <module>
          List[3]
      IndexError: list index out of range

      5. KeyError:字典關(guān)鍵字不存在

      Dic = {'1':'yes', '2':'no'}
      >>> Dic['3']
      Traceback (most recent call last):
        File "<pyshell#20>", line 1, in <module>
          Dic['3']
      KeyError: '3'

      6. IOError:輸入輸出錯(cuò)誤

      >>> f = open('abc')
      IOError: [Errno 2] No such file or directory: 'abc'

      7. AttributeError:訪問未知對(duì)象屬性

      >>> class Worker:
       def Work():
        print("I am working")
      
      >>> w = Worker()
      >>> w.a
      Traceback (most recent call last):
        File "<pyshell#51>", line 1, in <module>
          w.a
      AttributeError: 'Worker' object has no attribute 'a'

      8.ValueError:數(shù)值錯(cuò)誤

      >>> int('d')
      Traceback (most recent call last):
        File "<pyshell#54>", line 1, in <module>
          int('d')
      ValueError: invalid literal for int() with base 10: 'd'

      9. TypeError:類型錯(cuò)誤

      >>> iStr = '22'
      >>> iVal = 22
      >>> obj = iStr + iVal;
      Traceback (most recent call last):
        File "<pyshell#68>", line 1, in <module>
          obj = iStr + iVal;
      TypeError: Can't convert 'int' object to str implicitly

      10. AssertionError:斷言錯(cuò)誤

      >>> assert 1 != 1
      Traceback (most recent call last):
        File "<pyshell#70>", line 1, in <module>
          assert 1 != 1
      AssertionError

      11.MemoryError:內(nèi)存耗盡異常

      12. NotImplementedError:方法沒實(shí)現(xiàn)引起的異常

      class Base(object):
          def __init__(self):
              pass
      
          def action(self):
              #拋出異常,說明該接口方法未實(shí)現(xiàn)
              raise NotImplementedError

      13. LookupError:鍵、值不存在引發(fā)的異常

      LookupError異常是IndexError、KeyError的基類, 如果你不確定數(shù)據(jù)類型是字典還是列表時(shí),可以用LookupError捕獲此異常

      14. StandardError 標(biāo)準(zhǔn)異常

      除StopIteration, GeneratorExit, KeyboardInterrupt 和SystemExit外,其他異常都是StandarError的子類。

      錯(cuò)誤檢測與異常處理區(qū)別在于:錯(cuò)誤檢測是在正常的程序流中,處理不可預(yù)見問題的代碼,例如一個(gè)調(diào)用操作未能成功結(jié)束。

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多