If you have only two functions, foo () bar (), check out the other solutions. If you need to run many lines, try something like this example:
def foo(): raise Exception('foo_message') def bar(): print'bar is ok' def foobar(): raise Exception('foobar_message') functions_to_run = [ foo, bar, foobar, ] for f in functions_to_run: try: f() except Exception as e: print '[Warning] in [{}]: [{}]'.format(f.__name__,e)
Result:
[Warning] in [foo]: [foo_message] bar is ok [Warning] in [foobar]: [foobar_message]
source share