Python raise 单独一行的含义

在Python3中,单独的raise会重新触发前一个异常, 如果之前没有触发异常, 触发RuntimeError。

In [1]: try:
   ...:     1/0
   ...: except ZeroDivisionError:
   ...:     raise
   ...:
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-1-758acd4e90aa> in <module>
      1 try:
----> 2     1/0
      3 except ZeroDivisionError:
      4     raise
      5

ZeroDivisionError: division by zero

在框架中,常常以下面的形式出现

In [1]: try:
   ...:     1/0
   ...: except:
   ...:     raise
   ...:
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-1-a69156c10f2c> in <module>
      1 try:
----> 2     1/0
      3 except:
      4     raise
      5

ZeroDivisionError: division by zero

参考:

https://docs.python.org/3.7/reference/simple_stmts.html#the-raise-statement

你可能感兴趣的:(Python)