当前位置: 代码网 > it编程>前端脚本>Python > Python语法 raise ... from 用法示例详解

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

2025年07月29日 Python 我要评论
前言当程序出现错误时,系统会自动触发异常。python 也允许程序自行引发异常,自行引发异常使用 raise 语句来完成。使用raise抛出新的异常使用raise ... from ...抛出新的异常

前言

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

  • 使用 raise 抛出新的异常
  • 使用 raise ... from ... 抛出新的异常时,新的异常是由旧的异常表现的;
  • 使用 raise ... from none 抛出新的异常时,不会打印旧的异常(即禁止的异常关联)

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 用法内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com