有没有一种方法可以将此错误放入python中的变量中?

有没有一种方法可以将此错误放入python中的变量中?,python,try-catch,Python,Try Catch,我这里有代码: import traceback try: raise TypeError("Oups!") except Exception as err: try: raise TypeError("Again !?!") except: pass traceback.print_tb(err.__traceback__) 我不确定如何将错误放入具有相同格式的变量中。错误如下所示:

我这里有代码:

import traceback

try:
    raise TypeError("Oups!")
except Exception as err:
    try:
        raise TypeError("Again !?!")
    except:
        pass
    traceback.print_tb(err.__traceback__)
我不确定如何将错误放入具有相同格式的变量中。错误如下所示:

  File "C:/Users/user/PycharmProjects/project/file.py", line 4, in <module>
    raise TypeError("Oups!")
文件“C:/Users/user/PycharmProjects/project/File.py”,第4行,在
raise TypeError(“Oups!”)

有人能帮忙吗?

raise关键字向上抛出异常,在这里,当程序到达顶层时退出。 您可以像其他类型一样保存异常,因为它只是一个类。试试这个:
smthing=TypeError(“newerror”)

,它只返回
newerror
,而不是格式化错误。显然,它也适用于变量err。