python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python  raise ... from 用法

Python语法 raise ... from 用法示例详解

作者:上海-悠悠

Python中raise语句用于主动抛出异常,try-except捕获后可重抛,raise...from...关联异常链,from None则断开,影响错误信息显示,下面给大家介绍Python语法 raise ... from 用法示例,感兴趣的朋友一起看看吧

前言

当程序出现错误时,系统会自动触发异常。Python 也允许程序自行引发异常,自行引发异常使用 raise 语句来完成。

raise 引发异常

使用 raise 语句,主动引发异常,终止程序

x = 20
if not isinstance(x, str):
    raise Exception("value is not type of str")
else:
    print("hello")

运行结果:

Traceback (most recent call last):
  File "D:/demo/untitled1/demo/a.py", line 4, in <module>
    raise Exception("value is not type of str")
Exception: value is not type of str

当raise 抛出异常时,程序就会终止

try... except 捕获异常

使用try... except 捕获异常

x = [20, 3, 22, 11]
try:
    print(x[7])
except IndexError:
    print("index out of list")

运行后不会有异常
在捕获异常后,也可以重新抛一个其它的异常

x = [20, 3, 22, 11]
try:
    print(x[7])
except IndexError:
    print("index out of list")
    raise NameError("new exception ...")

输出结果:

Traceback (most recent call last):
  File "D:/demo/untitled1/demo/a.py", line 4, in <module>
    print(x[7])
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/demo/untitled1/demo/a.py", line 7, in <module>
    raise NameError("new exception ...")
NameError: new exception ...

在抛出异常的日志中,可以看到日志中对 IndexError 和 NameError之间是描述是 During handling of the above exception, another exception occurred,即在处理 IndexError 异常时又出现了 NameError 异常,两个异常之间没有因果关系。

raise ... from 用法

示例:

x = [20, 3, 22, 11]
try:
    print(x[7])
except IndexError as e:
    print("index out of list")
    raise NameError("new exception ...") from e

运行结果

Traceback (most recent call last):
  File "D:/demo/untitled1/demo/a.py", line 4, in <module>
    print(x[7])
IndexError: list index out of range

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:/demo/untitled1/demo/a.py", line 7, in <module>
    raise NameError("new exception ...") from e
NameError: new exception ...

在抛出异常的日志中,可以看到日志中对 IndexError和 NameError 之间是描述是 The above exception was the direct cause of the following exception,即因为 IndexError 直接异常导致了 NameError异常,两个异常之间有直接因果关系。

示例:

x = [20, 3, 22, 11]
try:
    print(x[7])
except IndexError as e:
    print("index out of list")
    raise NameError("new exception ...") from None

运行结果

Traceback (most recent call last):
  File "D:/demo/untitled1/demo/a.py", line 7, in <module>
    raise NameError("new exception ...") from None
NameError: new exception ...

在抛出异常的日志中,可以看到日志只打印了 NameError 而没有打印 IndexError

补充:python中异常处理--raise的使用

使用raise抛出异常
当程序出现错误,python会自动引发异常,也可以通过raise显示地引发异常。一旦执行了raise语句,raise后面的语句将不能执行。
演示raise用法

try:
     s = None
     if s is None:
         print "s 是空对象"
         raise NameError     #如果引发NameError异常,后面的代码将不能执行
     print len(s)  #这句不会执行,但是后面的except还是会走到
except TypeError:
     print "空对象没有长度"
s = None
if s is None:
    raise NameError
print 'is here?' #如果不使用try......except这种形式,那么直接抛出异常,不会执行到这里

到此这篇关于Python语法 raise ... from 用法示例详解的文章就介绍到这了,更多相关Python raise ... from 用法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文