[英]How do I do the exception without the traceback?
I have the function which you can see in the picture.我有你可以在图片中看到的功能。 How do I only return the last line only, when the exception is raised, without the other lines: traceback.., file"" etc.如何仅在引发异常时返回最后一行,而不返回其他行:traceback..、file"" 等。
def division(k, m):
try:
k / m
except TypeError:
raise ValueError ('hey') from None
return k/ m
If you want to hide the exception, use pass
instead of raising another exception.如果要隐藏异常,请使用pass
而不是引发另一个异常。
def division(k, m):
try:
return k/m
except TypeError:
pass
If the exception occurs, this will return None
by default.如果发生异常,默认情况下将返回None
。 You could replace pass
with a different return
statement that returns whatever you want to use instead.您可以用不同的return
语句替换pass
,该语句返回您想要使用的任何内容。
You shouldn't do the division again outside the try
, since that will just get the error that you wanted to hide again.你不应该在try
之外再次进行除法,因为那只会得到你想再次隐藏的错误。
Just put就放
import sys
sys.tracebacklimit = 0
Somewhere in your code.在您的代码中的某处。 It will apply to all of your exception tracebacks though.不过,它将适用于您的所有异常回溯。 I personally can't think of why I would do it, but knock yourself out!我个人想不出为什么我会这样做,但把自己打倒!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.