異常和異常類



一般來說,異常是指任何不尋常的條件。異常通常表示錯誤,但有時它們是故意放在程式中的,例如提前終止過程或從資源短缺中恢復。有許多內建異常,它們指示諸如讀取檔案末尾或除以零之類的條件。我們可以定義自己的異常,稱為自定義異常。

異常處理使您能夠優雅地處理錯誤並對其進行有意義的操作。異常處理有兩個組成部分:“丟擲”和“捕獲”。

識別異常(錯誤)

Python中發生的每個錯誤都會導致一個異常,該異常將是其錯誤型別標識的錯誤條件。

>>> #Exception
>>> 1/0
Traceback (most recent call last):
   File "<pyshell#2>", line 1, in <module>
      1/0
ZeroDivisionError: division by zero
>>>
>>> var = 20
>>> print(ver)
Traceback (most recent call last):
   File "<pyshell#5>", line 1, in <module>
      print(ver)
NameError: name 'ver' is not defined
>>> #Above as we have misspelled a variable name so we get an NameError.
>>>
>>> print('hello)

SyntaxError: EOL while scanning string literal
>>> #Above we have not closed the quote in a string, so we get SyntaxError.
>>>
>>> #Below we are asking for a key, that doen't exists.
>>> mydict = {}
>>> mydict['x']
Traceback (most recent call last):
   File "<pyshell#15>", line 1, in <module>
      mydict['x']
KeyError: 'x'
>>> #Above keyError
>>>
>>> #Below asking for a index that didn't exist in a list.
>>> mylist = [1,2,3,4]
>>> mylist[5]
Traceback (most recent call last):
   File "<pyshell#20>", line 1, in <module>
      mylist[5]
IndexError: list index out of range
>>> #Above, index out of range, raised IndexError.

捕獲/攔截異常

當您的程式中發生一些異常情況並且您希望使用異常機制來處理它時,您會“丟擲異常”。try 和 except 關鍵字用於捕獲異常。每當 try 塊中發生錯誤時,Python 都會查詢匹配的 except 塊來處理它。如果存在,則執行將跳轉到那裡。

語法

try:
   #write some code
   #that might throw some exception
except <ExceptionType>:
   # Exception handler, alert the user

try 子句中的程式碼將逐條語句執行。

如果發生異常,則將跳過 try 塊的其餘部分,並執行 except 子句。

try:
   some statement here
except:
   exception handling

讓我們編寫一些程式碼來檢視在程式中不使用任何錯誤處理機制時會發生什麼。

number = int(input('Please enter the number between 1 & 10: '))
print('You have entered number',number)

只要使用者輸入數字,上述程式就能正常工作,但是如果使用者嘗試輸入其他資料型別(如字串或列表)會發生什麼情況?

Please enter the number between 1 > 10: 'Hi'
Traceback (most recent call last):
   File "C:/Python/Python361/exception2.py", line 1, in <module>
      number = int(input('Please enter the number between 1 & 10: '))
ValueError: invalid literal for int() with base 10: "'Hi'"

現在 ValueError 是一個異常型別。讓我們嘗試使用異常處理重寫上面的程式碼。

import sys

print('Previous code with exception handling')

try:
   number = int(input('Enter number between 1 > 10: '))

except(ValueError):
   print('Error..numbers only')
   sys.exit()

print('You have entered number: ',number)

如果我們執行程式並輸入一個字串(而不是數字),我們可以看到我們得到了不同的結果。

Previous code with exception handling
Enter number between 1 > 10: 'Hi'
Error..numbers only

引發異常

要從您自己的方法中引發異常,您需要像這樣使用 raise 關鍵字

raise ExceptionClass(‘Some Text Here’)

讓我們舉個例子

def enterAge(age):
   if age<0:
      raise ValueError('Only positive integers are allowed')
   if age % 2 ==0:
      print('Entered Age is even')
   else:
      print('Entered Age is odd')

try:
   num = int(input('Enter your age: '))
   enterAge(num)
except ValueError:
   print('Only positive integers are allowed')

執行程式並輸入正整數。

預期輸出

Enter your age: 12
Entered Age is even

但是當我們嘗試輸入負數時,我們會得到,

預期輸出

Enter your age: -2
Only positive integers are allowed

建立自定義異常類

您可以透過擴充套件 BaseException 類或 BaseException 的子類來建立自定義異常類。

Custom Exception Class

從上圖我們可以看到,Python 中的大多數異常類都擴充套件自 BaseException 類。您可以從 BaseException 類或其子類派生自己的異常類。

建立一個名為 NegativeNumberException.py 的新檔案並編寫以下程式碼。

class NegativeNumberException(RuntimeError):
   def __init__(self, age):
      super().__init__()
      self.age = age

上面的程式碼建立了一個名為 NegativeNumberException 的新異常類,它只包含建構函式,該建構函式使用 super()__init__() 呼叫父類建構函式並設定年齡。

現在要建立您自己的自定義異常類,我們將編寫一些程式碼並匯入新的異常類。

from NegativeNumberException import NegativeNumberException
def enterage(age):
   if age < 0:
      raise NegativeNumberException('Only positive integers are allowed')

   if age % 2 == 0:
      print('Age is Even')

   else:
      print('Age is Odd')

try:
   num = int(input('Enter your age: '))
   enterage(num)
except NegativeNumberException:
   print('Only positive integers are allowed')
except:
   print('Something is wrong')

輸出

Enter your age: -2
Only positive integers are allowed

建立自定義異常類的另一種方法。

class customException(Exception):
   def __init__(self, value):
      self.parameter = value

   def __str__(self):
      return repr(self.parameter)
try:
   raise customException('My Useful Error Message!')
except customException as instance:
   print('Caught: ' + instance.parameter)

輸出

Caught: My Useful Error Message!

異常層次結構

內建異常的類層次結構為 -

+-- SystemExit 
+-- KeyboardInterrupt 
+-- GeneratorExit 
+-- Exception 
+-- StopIteration 
+-- StopAsyncIteration 
+-- ArithmeticError 
| +-- FloatingPointError 
| +-- OverflowError 
| +-- ZeroDivisionError 
+-- AssertionError 
+-- AttributeError 
+-- BufferError 
+-- EOFError 
+-- ImportError 
+-- LookupError 
| +-- IndexError 
| +-- KeyError 
+-- MemoryError 
+-- NameError 
| +-- UnboundLocalError 
+-- OSError 
| +-- BlockingIOError 
| +-- ChildProcessError 
| +-- ConnectionError 
| | +-- BrokenPipeError 
| | +-- ConnectionAbortedError 
| | +-- ConnectionRefusedError 
| | +-- ConnectionResetError 
| +-- FileExistsError 
| +-- FileNotFoundError 
| +-- InterruptedError 
| +-- IsADirectoryError 
| +-- NotADirectoryError 
| +-- PermissionError 
| +-- ProcessLookupError 
| +-- TimeoutError 
+-- ReferenceError 
+-- RuntimeError 
| +-- NotImplementedError 
| +-- RecursionError 
+-- SyntaxError 
| +-- IndentationError
| +-- TabError 
+-- SystemError 
+-- TypeError 
+-- ValueError 
| +-- UnicodeError 
| +-- UnicodeDecodeError 
| +-- UnicodeEncodeError 
| +-- UnicodeTranslateError 
+-- Warning 
+-- DeprecationWarning 
+-- PendingDeprecationWarning 
+-- RuntimeWarning 
+-- SyntaxWarning 
+-- UserWarning 
+-- FutureWarning 
+-- ImportWarning 
+-- UnicodeWarning 
+-- BytesWarning 
+-- ResourceWarning
廣告