Python中的超類Exception
是什麼?請爲我提供Python異常層次結構。在Python中是否有任何Exception超類,如Throwable Java Exception?
-1
A
回答
3
您正在尋找BaseException
。
用戶定義的異常類型應劃分爲Exception
。請參閱docs。
2
Exception
的基類:
>>> Exception.__bases__
(BaseException,)
Exception hierarchy從文檔證實,它是所有異常的基類:
BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- StopIteration
+-- ArithmeticError
| +-- FloatingPointError
...
捕獲所有異常的語法是:
try:
raise anything
except:
pass
注意:使用它非常非常謹慎,例如,你可以使用它在__del__
清理過程中的方法,當世界可能被半毀,並沒有其他的選擇。
的Python 2允許提高不是從BaseException
派生的異常:
>>> raise 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: exceptions must be old-style classes or derived from BaseException, not int
>>> class A: pass
...
>>> raise A
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
__main__.A: <__main__.A instance at 0x7f66756faa28>
它是固定在Python 3,強制執行的規則:
>>> raise 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: exceptions must derive from BaseException
+0
謝謝老闆的幫助........... –
+0
對我來說,'除了:'從來沒有成爲SyntaxError,它似乎不值得這個縮寫! (vs'除了BaseException:')。我想這是爲了方便和向後compat腳本(雖然我打賭大多數時間他們的意思'除了例外:')。 –
相關問題
- 1. Java中的Throwable和Exception
- 2. 如何製作jaxb-xjc使用的「Exception」類類Throwable?
- 3. 是否是java Exception class checked type?
- 4. Python是否具有拋出新Exception的Java等價物(「text here」)
- 5. IntelliJ中是否有「Break on Exception」?
- 6. MySql - Java - 找不到類Exception
- 7. python reraise/recatch exception
- 8. 在Python中,是否有對Exception的提升者的引用?
- 9. Java Exception Listener
- 10. 有沒有python exception notifier存在?
- 11. D編程語言中有哪些類成員可以使用Throwable和Exception?
- 12. 如何處理python中的try/exception塊?
- 13. Java IO Exception - Selenium GRID
- 14. 引發Exception類和Exception實例有區別嗎?
- 15. IMAGE EXCEPTION
- 16. 捕獲Exception類而不是Exception子類是不好的做法嗎?
- 17. 如何找出Exception的基類是否爲OperationCanceledException?
- 18. Exception類如何知道ArithmeticException類?
- 19. 沒有拋出Exception?
- 20. 在C#中的checked exception
- 21. TypeInitialization Exception
- 22. setBackgroundColor exception
- 23. FLUME EXCEPTION
- 24. 當僅有Exception類可以處理所有類型的異常時,其他Exception類的需求是什麼?
- 25. 要使用哪個Exception類?
- 26. 如何在Java編譯時鏈接Exception類
- 27. 是否有任何情況(在實踐中),在catch塊中拋出Exception是有用的?
- 28. Spring Java Exception Listener without web
- 29. 找不到數據Java exception
- 30. Kivy ScrollView exception raise Exception('ScrollView accept only one widget')
非常感謝您的幫助.. ... :-) –