title: Handling multiple exceptions in functions
body: <p>I have a tiny web-server written in Python 3 using <a href="https://docs.python.org/3/library/http.server.html" rel="nofollow noreferrer">http.server</a> which calls the function <code>translate()</code> in method <code>do_GET()</code> like this:</p>
  
  <pre><code>class httpd(BaseHTTPRequestHandler):
      def do_GET(self):
          self.wfile.write(bytes(f'{translate(var[0])}', 'utf-8'))
  </code></pre>
  
  <p>Now in this <code>translate()</code> function I have several conditional statements and <code>try</code> and <code>except</code> blocks roughly like this:</p>
  
  <pre><code>def translate(param):
      try:
          # do something
      except SomeError as some_err:
          print("Error: " % some_err)
          return ""
  
      if True:
          try:
              # do something
          except SomeOtherError as some_other_err:
              print("Error: " % some_other_err)
              return ""
          except SomeThirdError as some_third_err:
              print("Third error: " % some_third_err)
              return ""
      else:
          # additional try and except blocks which print an error and
          # return an empty string
  </code></pre>
  
  <p>The code above is simplified, but in principle I return an empty string if an exception happens and thus my web server returns nothing to client if an exception happens.</p>
  
  <p>Is there a more manageable way to handle this? Specifically, I'm looking to:</p>
  
  <ul>
  <li>Avoid catching each error via a separate <code>except</code> section, while still supporting an error message dependent on error type.</li>
  <li>Avoid writing multiple <code>try</code> / <code>except</code> statements, often nested, within my function.</li>
  </ul>
  
  <hr>
  
  <p><strong>Note:</strong> This is a copy of <a href="https://stackoverflow.com/questions/52901075/pythonic-way-to-handle-exceptions-in-functions">this now deleted question</a>. The solution from that post is included below, but other answers are welcome.</p>