如何在 Python 3 中打印异常? [英] How to print an exception in Python 3?
问题描述
现在,我在except Exception:
子句中捕获异常,然后执行print(exception)
.结果不提供任何信息,因为它总是打印
.我知道这曾经在 python 2 中工作,但我如何在 python3 中做到这一点?
Right now, I catch the exception in the except Exception:
clause, and do print(exception)
. The result provides no information since it always prints <class 'Exception'>
. I knew this used to work in python 2, but how do I do it in python3?
推荐答案
我猜您需要将 Exception
分配给一个变量.正如在 Python 3 教程中所示:
I'm guessing that you need to assign the Exception
to a variable. As shown in the Python 3 tutorial:
def fails():
x = 1 / 0
try:
fails()
except Exception as ex:
print(ex)
简单解释一下,as
是一个伪赋值关键字,用于某些复合语句中,用于将前面的语句赋值或别名给变量.
To give a brief explanation, as
is a pseudo-assignment keyword used in certain compound statements to assign or alias the preceding statement to a variable.
在这种情况下,as
将捕获的异常分配给一个变量,允许存储和稍后使用有关异常的信息,而不需要立即处理.
In this case, as
assigns the caught exception to a variable allowing for information about the exception to stored and used later, instead of needing to be dealt with immediately.
(这在 Python 3 Language 中有详细讨论参考:try
语句.)
(This is discussed in detail in the Python 3 Language Reference: The try
Statement.)
还有其他使用 as
的复合语句.第一个是 with
语句:
There are other compound statements that use as
. The first is the with
statement:
@contextmanager
def opening(filename):
f = open(filename)
try:
yield f
finally:
f.close()
with opening(filename) as f:
# ...read data from f...
此处,with
语句用于用 上下文管理器.这就像一个简洁的生成器包中的扩展 try...except...finally
语句,并且 as
语句将生成器生成的结果从上下文管理器分配给一个可扩展使用的变量.
Here, with
statements are used to wrap the execution of a block with methods defined by context managers. This functions like an extended try...except...finally
statement in a neat generator package, and the as
statement assigns the generator-produced result from the context manager to a variable for extended use.
(这在 Python 3 Language 中有详细讨论参考:with
声明.)
(This is discussed in detail in the Python 3 Language Reference: The with
Statement.)
从 Python 3.10 开始,match
语句也使用 as
:
As of Python 3.10, match
statements also use as
:
from random import randint
match randint(0, 2):
case 0|1 as low:
print(f"{low} is a low number")
case _:
print("not a low number")
match
语句采用一个表达式(在本例中为 randint(0, 2)
)并将其值与每个 case
分支进行比较一次,直到其中一个成功,此时它会执行该分支的块.在 case
分支中,如果分支成功,as
可用于将分支的值分配给变量.如果不成功,则不绑定.
match
statements take an expression (in this case, randint(0, 2)
) and compare its value to each case
branch one at a time until one of them succeeds, at which point it executes that branch's block. In a case
branch, as
can be used to assign the value of the branch to a variable if that branch succeeds. If it doesn't succeed, it is not bound.
(match
语句包含在 教程 并在 Python 中详细讨论3 语言参考:match
Statements.)
(The match
statement is covered by the tutorial and discussed in detail in the Python 3 Language Reference: match
Statements.)
最后,在导入模块时可以使用 as
,将模块别名为不同的(通常较短)名称:
Finally, as
can be used when importing modules, to alias a module to a different (usually shorter) name:
import foo.bar.baz as fbb
This is discussed in detail in the Python 3 Language Reference: The import
Statement.
这篇关于如何在 Python 3 中打印异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!