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

标签 python sqlite command-line-interface

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

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/

相关文章:

python - 复制的excel单元格值在另一个打印时不采用百分比值

python - 我什么时候应该在 python 中使用 uuid.uuid1() 和 uuid.uuid4()?

python - 一个多余但很酷的动态 python 结构以及关于装饰器、推导式、语法的问题

vb.net - 在 vb.net 和 sqlite 中将数据表保存回数据库

python - 将utf-8输入用于cmd Python模块

python - reshape 数组内不同维度的 numpy 数组

android - 在android中使用sqlite的正则表达式

android - sqlite选择中的撇号

macos - 环境 : node\r: No such file or directory with cordova cli

java - 有没有办法以编程方式从计算机访问短信?