gpt4 book ai didi

python - 在clone_saved_pa​​th()中捕获异常

转载 作者:太空宇宙 更新时间:2023-11-03 17:34:52 25 4
gpt4 key购买 nike

我正在尝试找出如何让该函数显示异常

clone_saved_path():

def clone_saved_path():

except OSError as e:
# If the error was caused because the source wasn't a directory
print('Directory not copied. Error: %s' % e)
print("got here. [4]")
except:
print("Another error occurred in clone_saved_path() ")
print("got here. [5]")

当我运行代码时,它会命中最后一个 except block 并输出:

print("Another error occurred in clone_saved_path() ")

我知道这听起来可能很基础,但我需要弄清楚如何显示实际异常。

最佳答案

https://docs.python.org/3/library/sys.html#sys.exc_info
“此函数返回一个包含三个值的元组,其中提供有关当前正在处理的异常的信息。”

所以你可以执行如下操作(此处建议:https://docs.python.org/3/tutorial/errors.html#handling-exceptions):

import sys
# ...
except:
print("Another error occurred in clone_saved_path() ")
print(sys.exc_info()[0]) # type of exception
raise

关于python - 在clone_saved_pa​​th()中捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31343494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com