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...
try/exceptorif/elif/elseblock to another one. This is like a rail junction: you go at full speed and go either left or right and are unable to move the train to another branch if you have already taken one.