python - 如何在 Python 中捕获我的异常引发并再次反弹
问题描述
我正在尝试引发一个句柄异常,抓住他并再次引发它。问题是我无法保留该异常并导致程序在我捕获异常时再次停止。
我已经尝试过这种方式和其他一些方式并没有成功
try:
if not (1 == 2):
raise ValueError("This is my exception")
except Exception as error:
raise ValueError(error) # Trying to throw the previous exception
提前致谢
解决方案
如果您希望引发被捕获的异常,您需要做的就是使用raise
try:
...
except:
raise # this will re-raise the exception that was caught
Python 3 允许您作为另一个异常的结果引发异常,从而为您提供有关根本原因的更多信息的回溯。
try:
...
exception Exception as e:
raise CustomException('message') from e
推荐阅读
- sql - 如果一个值存在,则选择记录,否则另一个
- r - 开销内存消耗的可能来源(R,data.table)
- java - java:找不到符号符号:类getLogger位置:类org.slf4j.LoggerFactory
- mysql - MySQL 加入 WHERE IN 并结合 NOT IN(类别)
- python-3.x - Python 3.x 到 Productivity 2000 PLC 通信
- php - 创建 HTML 正文后不接收电子邮件
- amazon-sagemaker - 如何使用 sagemaker java API 调用端点?
- javascript - 文件夹级脚本引用文档级脚本
- python - 到每个质心的 Kmeans 欧几里得距离避免从 DF 的其余部分拆分特征
- c# - 使用 MOQ 和 C# 模拟接口并调用原始实现方法