Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Propagate an exception through a Try/Except Block with multiple Excepts

Is there a way to propagate an exception in a try/except block from one except to the next?

I want to catch a specific error and then do a general error handling as well.

"raise" is letting the exception "bubble up" to an outside try/except, but not inside the try/except block that raised the error.

It should be ideally something like this:

import logging

def getList():
    try:
        newList = ["just", "some", "place", "holders"]
        # Maybe from something like: newList = untrustedGetList()

        # Faulty List now throws IndexError
        someitem = newList[100]

        return newList

    except IndexError:
        # For debugging purposes the content of newList should get logged.
        logging.error("IndexError occured with newList containing: \n%s",   str(newList))

    except:
        # General errors should be handled and include the IndexError as well!
        logging.error("A general error occured, substituting newList with backup")
        newList = ["We", "can", "work", "with", "this", "backup"]
        return newList

The problem I have is that when the IndexError gets catched with the first except, my general error handling in the second except block is not applied.

The only workaround I have for now is to include the general error handling code in the first block as well. Even if i wrap it in it's own functionblock it still seems less than elegant...

like image 732
Nimrod Avatar asked Dec 29 '16 14:12

Nimrod


1 Answers

You have two options:

  • Don't catch IndexError with a dedicated except .. block. You can always test manually for the type of exception in the general block by catching BaseException and assigning the exception to a name (here e):

    try:
        # ...
    except BaseException as e:
        if isinstance(e, IndexError):
            logging.error("IndexError occured with newList containing: \n%s",   str(newList))
    
        logging.error("A general error occured, substituting newList with backup")
        newList = ["We", "can", "work", "with", "this", "backup"]
        return newList
    
  • Use nested try..except statements and re-raise:

    try:
        try:
            # ...
        except IndexError:
            logging.error("IndexError occured with newList containing: \n%s",   str(newList))
            raise
    except:
        logging.error("A general error occured, substituting newList with backup")
        newList = ["We", "can", "work", "with", "this", "backup"]
        return newList
    
like image 64
Martijn Pieters Avatar answered Nov 20 '22 20:11

Martijn Pieters