【发布时间】:2021-12-26 07:48:05
【问题描述】:
我编写了一个函数 f(x),它要么返回字典,要么返回错误消息。
def myfunc():
try:
return dictionary
except BaseException as e:
return e
return_value = myfunc()
if return_value a dictionary:
do this
else:
do that
我需要检测另一个调用 f(x) 的函数 f(y) 中的返回类型。
我该怎么做?
提前致谢
【问题讨论】:
-
对此不太确定,但我想你可以在
myfunc()之外直接访问try ... catch,因为如果出错,该函数会将异常传递给调用者。 -
@j1-lee:异常不会传播到
myfunc()之外。 -
你不应该
return一个异常实例。你应该让它升起来。这就是异常如何工作的要点。
标签: python dictionary