Skip to main content
added 168 characters in body
Source Link
ftoyoshima
  • 502
  • 6
  • 13

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

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.

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
Source Link
ftoyoshima
  • 502
  • 6
  • 13

What is the appropriete way to handling exceptions inside a python method?

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.