如何隐藏内部错误堆栈[python]

2024-09-28 19:02:20 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的代码中,我希望防止显示内部错误回溯。 (在lua中,等效值为error("info",2)

在Python中也可以这样做吗

def asd():
  def efe(number = 2):
    try:
      number += 2
      print(number)
    except TypeError as e:
      failed = True
#      raise TypeError() # makes it even worse
    if failed:
      raise TypeError() # dont show internal stuff please
      # raise TypeError().with_traceback() # i guess use this is the way

  efe(number = "asd") # i want the end of Error Stack here

asd()

印刷品

Traceback (most recent call last):
  File "main.py", line 15, in <module>
    asd()
  File "main.py", line 13, in asd
    efe(number = "asd") # i want the end of Error Stack here
  File "main.py", line 10, in efe
    raise TypeError() # dont show internal stuff please
TypeError

Tags: theinpynumbermaindefshowline