[英]Why do I have to redo a try-except statement when calling a function that does this exactly?
I have the following problem: I have specified a method for a given class which handles two different Errors with the same user-defined exception (see the method below).我有以下问题:我为给定的类指定了一个方法,该方法处理具有相同用户定义异常的两个不同错误(请参阅下面的方法)。
def get_route(self, source, target):
try:
n = nx.shortest_path(self.graph, source, target)
return n
except (nx.NetworkXNoPath, nx.NodeNotFound):
raise NoSuchFlight
So now, I want to further define a function which calls the above method.所以现在,我想进一步定义一个调用上述方法的函数。 I have noticed that the code works when I redo a try-except statement I do not really understand (see below).
我注意到当我重做一个我不太理解的 try-except 语句时,代码可以工作(见下文)。
def get_airlines_per_route(airlines_dict, source, target, max_intermediate_stops):
lst = []
for airline in airlines_dict.values():
try:
path = airline.get_route(source, target)
if (len(path) - 2) <= max_intermediate_stops:
lst.append(airline)
except:
NoSuchFlight
return lst
However, I have no idea why this works.但是,我不知道为什么会这样。 Anyway is my code correct?
无论如何,我的代码正确吗? And moreover, how would you guys have tackled this problem?
此外,你们将如何解决这个问题?
Thanks for any advice.感谢您的任何建议。
Update:更新:
The user-defined exception:用户定义的异常:
class NoSuchFlight(Exception):
pass
Intended way:打算的方式:
def get_airlines_per_route(airlines_dict, source, target, max_intermediate_stops):
lst = []
for airline in airlines_dict.values():
path = airline.get_route(source, target)
if (len(path) - 2) <= max_intermediate_stops:
lst.append(airline)
return lst
the second try except
will catch all the Exceptions
this why your code is working, I suggest to use:第二次
try except
将捕获所有Exceptions
这就是为什么您的代码正在工作,我建议使用:
try:
path = airline.get_route(source, target)
if (len(path) - 2) <= max_intermediate_stops:
lst.append(airline)
except NoSuchFlight:
# log or something else
It works but you should not have code like that.它有效,但你不应该有这样的代码。
In the inner function you raise a custom exception in abnormal conditions.在内部函数中,您在异常情况下引发自定义异常。 This part is correct, even if it is more common to encapsulate the original error for eventual further analysis.
这部分是正确的,即使更常见的是封装原始错误以供最终进一步分析。 So I would probably use:
所以我可能会使用:
except (nx.NetworkXNoPath, nx.NodeNotFound) as e:
raise NoSuchFlight(e)
The outer code is really weird: except:
will catch any exception.外部代码真的很奇怪:
except:
会捕获任何异常。 This is bad practice because you should not catch exception that you do not intend to process.这是不好的做法,因为您不应捕获不打算处理的异常。 For example, an IO error because of a physical disk problem should probably not be ignored.
例如,由于物理磁盘问题导致的 IO 错误可能不应被忽略。
And the following line, NoSuchFlight
is close to a no-op: it just names an exception.下面这行,
NoSuchFlight
接近于无操作:它只是命名一个异常。 You can try to type Exception
in a python interpretor and it will just display the class name:您可以尝试在 python 解释器中输入
Exception
,它只会显示类名:
>>> Exception
<class 'Exception'>
without raising anything...没有提出任何...
If you just want to ignore erroneous routes, you should use:如果你只想忽略错误的路由,你应该使用:
def get_airlines_per_route(airlines_dict, source, target, max_intermediate_stops):
lst = []
for airline in airlines_dict.values():
try:
path = airline.get_route(source, target)
if (len(path) - 2) <= max_intermediate_stops:
lst.append(airline)
catch NoSuchFlight: # just skip in case of a NoSuchFlight
pass
return lst
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.