Python异常对象Exception基础类异常捕捉
短信预约 -IT技能 免费直播动态提醒
异常对象
Python中遇到错误后,会引发异常。
Python中使用异常对象来表示异常情况。
如果异常对象未被处理或者捕捉,程序就会用所谓的回溯(traceback)来终止执行。
下面是一个例子:
def func1():
raise Exception
if __name__ == "__main__":
func1()
执行之后报错:
(venv) E:\Codes\python_everything\begining-python\class="lazy" data-src\08>list8-2.py
Traceback (most recent call last):
File "E:\Codes\python_everything\begining-python\class="lazy" data-src\08\list8-2.py", line 19, in <module>
func1()
File "E:\Codes\python_everything\begining-python\class="lazy" data-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\class="lazy" data-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\class="lazy" data-src\08>list8-3.py
Traceback (most recent call last):
File "E:\Codes\python_everything\begining-python\class="lazy" data-src\08\list8-3.py", line 27, in <module>
func1()
File "E:\Codes\python_everything\begining-python\class="lazy" data-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\class="lazy" data-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类异常捕捉的资料请关注编程网其它相关文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341