Suppose I have a function and, depending on its inputs, it must "advise" the caller function that something went wrong:
def get_task(msg, chat):
task_id = int(msg)
query = db.SESSION.query(Task).filter_by(id=task_id, chat=chat)
try:
task = query.one()
except sqlalchemy.orm.exc.NoResultFound:
return "_404_ error"
return task
Notice at the except block I want to pass something that the caller function can handle and stop its execution if it's necessary, otherwise, it will return the right object.
def something_with_the_task(msg, chat):
task = get_task(msg, chat)
if task == "_404_ error":
return
#do some stuff with task
raisean exception and catch it in the caller function.raiseit, given you have no way to recover from the lower-level failure.