找到關於 Python 的10786 篇文章

如何在 Python 中捕獲 NotImplementedError 異常?

Rajendra Dharmkar
更新於 2020年2月12日 10:36:48

1K+ 次檢視

使用者定義的基類可以引發 NotImplementedError 來指示子類需要定義方法或行為,模擬介面。此異常派生自 RuntimeError。在使用者定義的基類中,抽象方法應在需要派生類重寫方法時引發此異常。示例import sys try: class Super(object): @property def example(self): raise NotImplementedError("子類應該實現這個!") s = Super() print s.example except Exception as e: print e print sys.exc_type輸出子類應該實現這個!

如何在 Python 中捕獲 ImportError 異常?

Rajendra Dharmkar
更新於 2020年6月12日 07:42:03

4K+ 次檢視

當無法匯入模組或模組的成員時,會引發 ImportError。ImportError 可能在兩種情況下引發。如果模組不存在。示例import sys try: from exception import myexception except Exception as e: print e print sys.exc_type輸出No module named exception 如果使用 from X import Y 並且在模組 X 中找不到 Y,則會引發 ImportError。示例 import sys try: from time import datetime except Exception as e: print e print sys.exc_type輸出 cannot import name datetime

如何在 Python 中捕獲 SystemExit 異常?

Rajendra Dharmkar
更新於 2020年2月12日 10:39:46

2K+ 次檢視

在 python 文件中,SystemExit 不是 Exception 類的子類。BaseException 類是 SystemExit 的基類。因此,在給定的程式碼中,我們將 Exception 替換為 BaseException 以使程式碼工作示例try: raise SystemExit except BaseException: print "它有效!"輸出它有效!該異常繼承自 BaseException 而不是 StandardError 或 Exception,以便不會被意外地捕獲到捕獲 Exception 的程式碼。我們最好這樣編寫程式碼示例try: raise SystemExit except SystemExit: print "它有效!"輸出它有效!

如何在 Python 中捕獲 StopIteration 異常?

Rajendra Dharmkar
更新於 2020年2月12日 10:40:43

1K+ 次檢視

當迭代器完成時,它的 next 方法會引發 StopIteration。此異常不被認為是錯誤。我們如下重寫給定的程式碼以捕獲異常並瞭解其型別。示例import sys try: z = [5, 9, 7] i = iter(z) print i print i.next() print i.next() print i.next() print i.next() except Exception as e: print e print sys.exc_type 輸出 5 9 7

如何在 Python 中捕獲 StandardError 異常?\\\

Rajendra Dharmkar
更新於 2020年2月12日 10:41:26

425 次檢視

存在 Exception 類,它是 StopIteration、StandardError 和 Warning 的基類。所有標準錯誤都派生自 StandardError。一些標準錯誤,如 ArithmeticErrror、AttributeError、AssertionError,都派生自基類 StandardError。當屬性引用或賦值失敗時,會引發 AttributeError。例如,當嘗試引用不存在的屬性時:我們重寫給定的程式碼並捕獲異常並瞭解其型別。示例import sys try: class Foobar: def __init__(self): self.p = 0 f = Foobar() print(f.p) print(f.q) except Exception as e: print e print sys.exc_type print '這是一個 StandardError 異常的示例'輸出0 Foobar ... 閱讀更多

如何在 Python 中捕獲 FloatingPointError 異常?

Rajendra Dharmkar
更新於 2020年2月12日 10:42:09

1K+ 次檢視

當啟用浮點異常控制 (fpectl) 時,浮點運算導致錯誤時,會引發 FloatingPointError。啟用 fpectl 需要使用 --with-fpectl 標誌編譯的直譯器。給定的程式碼如下重寫,以處理異常並查詢其型別。示例import sys import math import fpectl try: print '控制關閉:', math.exp(700) fpectl.turnon_sigfpe() print '控制開啟:', math.exp(1000) except Exception as e: print e print sys.exc_type 輸出控制關閉:1.01423205474e+304 控制開啟:in math_1

如何在 Python 中捕獲 ZeroDivisionError 異常?

Rajendra Dharmkar
更新於 2020年2月12日 10:42:53

463 次檢視

當除法運算的分母中出現零時,會引發 ZeroDivisionError。我們如下重寫給定的程式碼以處理異常並查詢其型別。示例import sys try: x = 11/0 print x except Exception as e: print sys.exc_type print e輸出integer division or modulo by zero

如何在 Python 中使用 Exception 捕獲 ValueError?

Manogna
更新於 2020年6月12日 07:56:10

299 次檢視

當函式接收具有正確型別但無效值的 value 時,使用 ValueError。給定的程式碼可以按如下方式重寫以處理異常並查詢其型別。示例import sys try: n = int('magnolia') except Exception as e: print e print sys.exc_type輸出invalid literal for int() with base 10: 'magnolia'

如何在 Python 中捕獲 LookupError 異常?

Manogna
更新於 2020年2月12日 10:54:12

1K+ 次檢視

LookupError 異常是當找不到某些內容時引發的錯誤的基類。當對映或序列上使用的鍵或索引無效時引發的異常的基類:IndexError、KeyError。當序列引用超出範圍時,會引發 IndexError。給定的程式碼如下重寫,以捕獲異常並查詢其型別示例import sys try: foo = [a, s, d, f, g] print foo[5] except IndexError as e: print e print sys.exc_type輸出C:/Users/TutorialsPoint1~.py list index out of range

如何在 Python 中捕獲 EnvironmentError 異常?

Manogna
更新於 2020年2月12日 10:53:26

381 次檢視

EnvironmentError 是來自 Python 外部(作業系統、檔案系統等)的錯誤的基類。EnvironmentError 異常是 StandarError 類的子類。它是 IOError 和 OSError 異常的基類。它實際上並沒有像它的子類錯誤 IOError 和 OSError 那樣引發。任何 IOError 或 OSError 的示例也應該是 Environment Error 的示例。示例import sys try: f = open ( "JohnDoe.txt", 'r' ) except Exception as e: print e print sys.exc_type輸出[Errno 2] No such file or directory: 'JohnDoe.txt'

廣告
© . All rights reserved.