36

I find myself having this sort of pattern over and over:

variable = ""
try:
    variable = ... do some file loading stuff ...
except:
    variable = ""

Is there any way to condense this into a single expression? Like with if-else statements you can turn:

variable = ""
if something:
    variable = somethingelse
else:
    variable = ""

into

variable = somethingelse if something else ""

Is there any equivalent thing for try-catch?

5
  • 1
    If an exception happens inside the try block when assigning variable then it won't get assigned, and will already be an empty string when you hit the except block. It may not shorten your code by much, but there's no need to assign it the value it already holds... Aug 17, 2011 at 4:40
  • 2
    Yes, but then he needs to add pass to the except, so it's still the same number of lines. It's the pre-try-except initialization that is unecessary.
    – agf
    Aug 17, 2011 at 4:43
  • 1
    short answer, Big NO. Take exception as serious error than looking for syntactic sugar to coat it. Add one more thing that is finally: no if else if else sort now.
    – user90150
    Aug 17, 2011 at 6:15
  • 1
    The problem with exceptions is that they aren't serious errors. When loading files from disk, for example, the proper way of checking if a file is there before loading is to go ahead and load it, and catch the exception if it doesn't exist.
    – Li Haoyi
    Aug 17, 2011 at 7:12
  • Yeah, in Python the EAFP coding style is common and preferable to LBYL, so exceptions definitely aren't serious errors.
    – Trevor Jex
    Nov 2, 2018 at 18:48

8 Answers 8

28

Since agf already provided the approach I'd recommend, here's a version of his routine with a couple of minor enhancements:

def try_except(success, failure, *exceptions):
    try:
        return success()
    except exceptions or Exception:
        return failure() if callable(failure) else failure

This version:

  1. Lets you specify exactly which exceptions will be caught as additional optional arguments. You should always catch the minimum set of exceptions that will do the job and let exceptions you can't handle bubble up to the caller.

  2. Supports the use of a plain value as well as a function for the failure value. This saves you having to use a lambda in a lot of cases. (Of course, instead of lambda: '' you can just use str.)

5
  • 1
    I like this. What about exceptions = exceptions or Exception or taking a list with a default of Exception (or just exceptions = Exception without the tuple) as refinements? Or return failure if callable(failure) else failure? (I like one liners too, like the OP).
    – agf
    Aug 17, 2011 at 7:13
  • I see it now -- remove that line completely and just except exceptions or Exception: -- inline and no need for the tuple. The same number of lines as my version with two extra features! It's even backwards compatible with my version.
    – agf
    Aug 17, 2011 at 14:54
  • As a bonus, it moves all the extra code into the exceptional path, so the more common success path is as fast is possible.
    – kindall
    Aug 17, 2011 at 17:04
  • seems not work for python3.7:``` def a(b): try: return b except Exception: return 0 c=a(int("a")) Traceback (most recent call last): File "<input>", line 1, in <module> ValueError: invalid literal for int() with base 10: 'a' c=a(int("ff")) Traceback (most recent call last): File "<input>", line 1, in <module> ValueError: invalid literal for int() with base 10: 'ff'```
    – daixiang0
    Sep 20, 2019 at 12:01
  • How do we pass the parameters to success ans failure?
    – JJoao
    May 3 at 7:30
19
def try_except(success, failure):
    try:
        return success()
    except:
        return failure()

variable = try_except(do_some_file_loading_stuff, lambda: '')

I think the code is self explanatory. It returns the value returned by success unless there is an error, then it returns the value returned by failure. If do_some_file_loading_stuff is an expression rather than just a function call, wrap it in a lambda as well.

Edit: @kindall and I improved his version a bit so it's just as fast as mine, can be called exactly the same if you want, has more features, and is the same number of lines. Use it!

def try_except(success, failure, *exceptions):
    try:
        return success()
    except exceptions or Exception:
        return failure() if callable(failure) else failure
16

Here's a context manager that provides a little bit of a shortcut:

from contextlib import contextmanager

@contextmanager
def catch(*exceptions, **kwargs):
    try:
        yield kwargs.get("default", None)
    except exceptions or Exception:
        pass

Usage:

with catch(ZeroDivisionError, default=0) as x:
    x = 3 / 0              # error

print x                    # prints 0, the default

The basic idea here is that the context manager returns whatever default value you pass it, which is then assigned to the variable you specified in the with statement's as clause. Then, inside the context, you execute a statement that tries to assign to that same variable. If it raises an exception, the context manager catches and silently ignores it, but since the assignment didn't happen, the default value remains.

Might be particularly useful for multi-line calculations that nevertheless result in a single value.

1
4

Unfortunately, no, there is no language construct for it and I do not know any really legible and concise idiom either. I always wanted something like that. Some time ago someone gave me reasons to Python does not have something like variable = function_cal() except "" but they were not very convincing and I still miss this language construct :)

1

Obviously, the latter way of doing it is more succinct and is preferred by many programmers like you.

While the language itself doesn't provide a "one-line try-except-catch" for whatever the reasons, all good and necessary, I presume, you can generally accomplish this, by changing the program, a little; like for example:

try:
    a_var = a_dict.get('abcd')
except a_dict.KeyError:
    a_var = ''

into:

a_var = a_dict.get('abcd',default='')

and similarly for the DB queries;

try:
    a_qs = Model.objects.get(id=42)
except Model.DoesNotExist:
    a_qs = Model.objects.create(id=42)

with

a_qs = Model.objects.get_or_create(id=42,**kwargs)

and add similar API's to your own programs where possible. try-except is rather "cheap" in Python and is exception based programming is preferred over the "check-first" approach generally suggested in Java like languages because of the expensive nature of the exception handling. So, you should rather wrap this "catching thing" in a method/function and call that every where, like for example the dict and other constructs do.

0

There is no easy way to simplify a try/catch statement like the if/else example, but I want to point out that python's "with" statement introduced in python2.5 made a lot of interfaces to file/db io try catch statements simpler and exception safe. IO operations tend to be where a lot of try/catch statements are used.

with open("myfile.txt", "r") as f:
   # Do stuff with f

instead of

try:
   f = open("myfile.txt", "r")
   # Do stuff with f
except:
   pass
finally:
   if f: f.close
3
  • hmmm I'm not familiar with the with operator. I shall have to look it up
    – Li Haoyi
    Aug 17, 2011 at 7:12
  • What is that catch keyword. I'm not sure that's a real thing. Aug 18, 2011 at 14:13
  • oh yeah, that's meant to be except, clearly the above code wasn't tested :P
    – Charles Ma
    Aug 19, 2011 at 11:42
0

Inspired by with-handlers in Racket:

def with_handlers(handlers: Dict[Type[Exception], Callable[[Exception], S]], f: Callable[[], T]) -> Union[S, T]:
    try:
        return f()
    except tuple(handlers.keys()) as err:
        return handlers[type(err)](err)

handlers contains all expected exceptions and their corresponding handlers.

And here is a catch function, which also supports else and final clauses:

def catch(
        try_clause: Callable[[], T],
        except_clauses: Dict[Type[Exception], Callable[[Exception], S]],
        else_clause: Optional[Callable[[T], R]] = None,
        final_clause: Optional[Callable[[Union[T, S]], None]] = None) -> Union[R, S, T]:
    try:
        ret = try_clause()
    except tuple(except_clauses.keys()) as err:
        handled = except_clauses[type(err)](err)
        return handled
    else:
        return ret if else_clause is None else else_clause(ret)
    finally:
        if final_clause is not None:
            try:
                final_clause(ret)
            except NameError:
                final_clause(handled)

In Python, try statements do not introduce their own scope, but here try_clause is a function, so else_clause needs a parameter to receive information from try_clause. Similarly, final_clause needs two parameters.

0

Following a variant of @kindall solution with parameters:

def OR(planA, planB, *exceptions):
    def x (*args):
        try: return planA(*args)
        except exceptions or Exception:
            return planB(*args) if callable(planB) else planB
    return x

Usage:

### d = OR( operator.truediv, "infinity")
d = OR( lambda x,y : x/y , "infinity")

print(d(4,8))                                 ## 0.5
print(d(4,0))                                 ## infinity
print(OR( operator.truediv, "infinity")(4,8)) ## 0.5
print(OR( operator.truediv, "infinity")(4,0)) ## infinity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.