在 clone_saved_path() 中捕获异常

Catching an exception in clone_saved_path()

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

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() ")

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

https://docs.python.org/3/library/sys.html#sys.exc_info
"This function returns a tuple of three values that give information about the exception that is currently being handled."

因此您可以执行如下操作(此处建议: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