title: Why do I have to redo a try-except statement when calling a function that does this exactly?
body: <p>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).</p>
  
  <pre><code>def get_route(self, source, target):
  
      try:
          n = nx.shortest_path(self.graph, source, target)
          return n
  
      except (nx.NetworkXNoPath, nx.NodeNotFound):
          raise NoSuchFlight
  </code></pre>
  
  <p>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).</p>
  
  <pre><code>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) &lt;= max_intermediate_stops:
                  lst.append(airline)
  
          except:
              NoSuchFlight
  
      return lst
  </code></pre>
  
  <p>However, I have no idea why this works. Anyway is my code correct? And moreover, how would you guys have tackled this problem?</p>
  
  <p>Thanks for any advice.</p>
  
  <p>Update:</p>
  
  <p>The user-defined exception:</p>
  
  <pre><code>class NoSuchFlight(Exception):
      pass
  </code></pre>
  
  <p>Intended way:</p>
  
  <pre><code>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) &lt;= max_intermediate_stops:
              lst.append(airline)
  
      return lst
  </code></pre>