Python:call";尝试除“外”;仅围绕“一个”的某些部分;如有其他",;块
有许多类似的问题,但似乎没有一个能完全解决我的情况。我发现自己想写一些类似的代码:Python:call";尝试除“外”;仅围绕“一个”的某些部分;如有其他",;块,python,exception,Python,Exception,有许多类似的问题,但似乎没有一个能完全解决我的情况。我发现自己想写一些类似的代码: try: if a: do_thing_a() elif b: do_thing_b() else: raise ValueError('thing not recognised!') except ValueError as e: handle_error() 问题是我实际上不想在这里捕捉ValueError('thingnotr
try:
if a:
do_thing_a()
elif b:
do_thing_b()
else:
raise ValueError('thing not recognised!')
except ValueError as e:
handle_error()
问题是我实际上不想在这里捕捉ValueError('thingnotrecognized!')
错误。我希望它传播起来。但是我确实想捕获可能由do\u thing\u a()
或do\u b()
引起的ValueError
s。感觉应该有更好的方法来处理这个问题,但我唯一能想到的是在“if”语句的每个部分中都有单独的“try”块:
if a:
try:
do_thing_a()
except ValueError as e:
handle_error()
elif b:
try:
do_thing_b()
except ValueError as e:
handle_error()
else:
raise ValueError('thing not recognised!')
我不喜欢重复,我觉得这更令人困惑,可读性也更低,但我看不到其他选择。这里有更好的解决办法吗?我想我可以定义一个特殊的异常
unrecognedThingError
,如果我将try except
语句包装在整个if else
块中,我知道它不会被捕获,然后分别作为ValueError
重新提出(我需要一个ValueError
,因为其他一些外部代码想要捕获特定的异常类型)。这可能更好,尽管感觉有些过分…我不认为您试图实现的目标是可能的,但是,您可以做的是检查引发的异常是否来自else
块,并执行以下操作:
try:
if a:
do_thing_a()
elif b:
do_thing_b()
else:
raise ValueError('thing not recognised!')
except ValueError as e:
if str(e) == 'thing not recognised!':
raise ValueError('thing not recognised!')
handle_error()
try:
if a:
do_thing_a()
elif b:
do_thing_b()
else:
raise Exception('thing not recognised!')
except ValueError as e:
handle_error()
它通过比较异常消息来工作,如果异常消息与您手动引发的消息相同,它将在except
块中引发新异常。但是,我建议如下:
try:
if a:
do_thing_a()
elif b:
do_thing_b()
else:
raise ValueError('thing not recognised!')
except ValueError as e:
if str(e) == 'thing not recognised!':
raise ValueError('thing not recognised!')
handle_error()
try:
if a:
do_thing_a()
elif b:
do_thing_b()
else:
raise Exception('thing not recognised!')
except ValueError as e:
handle_error()
通过这种方式,
except
块将不会捕获异常,因为它不是ValueError异常。我不认为您试图实现的是可能的,但是,您可以做的是检查引发的异常是否来自else
块,并执行以下操作:
try:
if a:
do_thing_a()
elif b:
do_thing_b()
else:
raise ValueError('thing not recognised!')
except ValueError as e:
if str(e) == 'thing not recognised!':
raise ValueError('thing not recognised!')
handle_error()
try:
if a:
do_thing_a()
elif b:
do_thing_b()
else:
raise Exception('thing not recognised!')
except ValueError as e:
handle_error()
它通过比较异常消息来工作,如果异常消息与您手动引发的消息相同,它将在except
块中引发新异常。但是,我建议如下:
try:
if a:
do_thing_a()
elif b:
do_thing_b()
else:
raise ValueError('thing not recognised!')
except ValueError as e:
if str(e) == 'thing not recognised!':
raise ValueError('thing not recognised!')
handle_error()
try:
if a:
do_thing_a()
elif b:
do_thing_b()
else:
raise Exception('thing not recognised!')
except ValueError as e:
handle_error()
通过这种方式,
except
块将不会捕获异常,因为它不是ValueError异常。这感觉就像是在这里重载ValueError。
我认为你的想法是对的,但有一个例外。
例如:
这真的感觉像是在这里重载ValueError。 我认为你的想法是对的,但有一个例外。 例如:
在第一个示例中,您可以在if中调整
raise e
,而不是创建一个新的ValueError
异常。我认为第二个示例不会回答这个问题,因为他们希望调用代码中出现ValueError
。在第一个示例中,您可以在if中调整raise e
,而不是创建一个新的ValueE>错误
异常。我认为第二个异常没有回答这个问题,因为他们希望调用代码中出现一个ValueError
。是的,这就是我现在所做的,我想没有比这更好的了。我想,让这个额外的异常类仅仅为了这一个目的而四处浮动并不是很糟糕。我想这是最好的方法sambiguate在代码中引发了错误。很难说,因为我假设您稍微擦洗了代码,但ValueError
只是觉得上下文不正确。是的,这只是一个玩具示例。是的,对于不同的东西,感觉ValueError太多了,但我正在更新其他人编写的一些现有代码,不想更新o更改太多的内容,以免我不得不重写整个软件包;)。是的,这就是我现在所做的,我想不出更好的了。我想,让这个额外的异常类仅仅为了这个目的而浮动并不是件坏事。这是消除代码中引发的错误歧义的最佳方法。很难说,因为我假设你稍微擦洗了一下代码,但是ValueError
感觉上下文不对。是的,这只是一个玩具示例。是的,对于不同的东西来说,感觉有太多的ValueErrors,但我正在更新一些别人编写的现有代码,不想更改太多内容,以免我不得不重写整个包;)。