Python异常对象Exception基础类异常捕捉
作者:jiangwei0512
这篇文章主要为大家介绍了Python异常对象异常捕捉及Exception基础类,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
异常对象
Python中遇到错误后,会引发异常。
Python中使用异常对象来表示异常情况。
如果异常对象未被处理或者捕捉,程序就会用所谓的回溯(traceback)来终止执行。
下面是一个例子:
def func1(): raise Exception if __name__ == "__main__": func1()
执行之后报错:
(venv) E:\Codes\python_everything\begining-python\src\08>list8-2.py Traceback (most recent call last): File "E:\Codes\python_everything\begining-python\src\08\list8-2.py", line 19, in <module> func1() File "E:\Codes\python_everything\begining-python\src\08\list8-2.py", line 15, in func1 raise Exception Exception
异常有不同的类型,Exception是基础类,下面还有各种子类:
+-- Exception +-- StopIteration +-- StandardError | +-- BufferError | +-- ArithmeticError | | +-- FloatingPointError | | +-- OverflowError | | +-- ZeroDivisionError | +-- AssertionError | +-- AttributeError | +-- EnvironmentError | | +-- IOError | | +-- OSError | | +-- WindowsError (Windows) | | +-- VMSError (VMS) | +-- EOFError | +-- ImportError | +-- LookupError | | +-- IndexError | | +-- KeyError | +-- MemoryError | +-- NameError | | +-- UnboundLocalError | +-- ReferenceError | +-- RuntimeError | | +-- NotImplementedError | +-- SyntaxError | | +-- IndentationError | | +-- TabError | +-- SystemError | +-- TypeError | +-- ValueError | +-- UnicodeError | +-- UnicodeDecodeError | +-- UnicodeEncodeError | +-- UnicodeTranslateError
还可以通过继承Exception来实现自己的类:
class SomeError(Exception): pass def func2(): raise SomeError if __name__ == "__main__": func2()
异常捕捉
异常可以被捕捉,需要使用try...except...语句:
class SomeError(Exception): pass def func2(): raise SomeError if __name__ == "__main__": try: func2() except SomeError: print("Some error happened")
这里捕捉到了参数,所以就不会回溯:
(venv) E:\Codes\python_everything\begining-python\src\08>list8-3.py Some error happened
但是如果是其它的异常:
def func1(): raise Exception class SomeError(Exception): pass def func2(): raise SomeError if __name__ == "__main__": try: func1() except SomeError: print("Some error happened")
这里的Exception就没有被捕获,所以还是会回溯:
(venv) E:\Codes\python_everything\begining-python\src\08>list8-3.py Traceback (most recent call last): File "E:\Codes\python_everything\begining-python\src\08\list8-3.py", line 27, in <module> func1() File "E:\Codes\python_everything\begining-python\src\08\list8-3.py", line 15, in func1 raise Exception Exception
如果没有出现异常,则会继续往下执行,但是这里可以有else语句来执行没有异常时可以执行的语句,使整个逻辑更通顺:
if __name__ == "__main__": try: print("No exception") except SomeError: print("Some error happened") else: print("No error happened")
结果如下:
(venv) E:\Codes\python_everything\begining-python\src\08>list8-3.py No exception No error happened
但是有时即使发生了无法捕捉的异常,也想要执行一些语句,这个时候就可以使用finally语句:
def func1(): raise Exception class SomeError(Exception): pass def func2(): raise SomeError if __name__ == "__main__": try: func1() except SomeError: print("Some error happened") else: print("No error happened") finally: print("Do something")
Python中更倾向于使用try...except...这样的语句来代替if...else..语句。
以上就是Python异常对象异常捕捉及Exception基础类的详细内容,更多关于Python对象Exception类异常捕捉的资料请关注脚本之家其它相关文章!