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

时间:2015-07-10 14:35:10

标签: 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块并输出:

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

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

1 个答案:

答案 0 :(得分:1)

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