How not to stop the execution of other function in python in case of Exception/Error
<p>I have a script in python which works as shown below. Each function performs a completely different task and not related to each other. My problem is if <strong>function2()</strong> is having an issue during the execution process then <strong>function3()</strong>, <strong>function4()</strong>, <strong>function5()</strong> will not execute. I know you will say to handle this by catching the exception (try..except) but then i have to catch every exception which is not i am looking for. In a nutshell how do i code where my other functions are not impacted if any of the function is having issue. Ideally it should exclude that problematic function and let the other function to execute.</p>

<pre><code>def function1():
    some code

def function2():
    some code

def function3():
    some code

def function4():
    some code

def function5():
    some code

if __name__ == '__main__':
    function1()
    function2()
    function3()
    function4()
    function5()
</code></pre>

<p>No need to write multiple <code>try/except</code>. Create a list of your function and execute them. For example, you code should be like:</p>

<pre><code>if __name__ == '__main__':
    func_list = [function1, function2, function3, function4, function5]

    for my_func in func_list:
        try:
            my_func()
        except:
            pass
</code></pre>

<hr>

<p>OR, create a <em>decorator</em> and add that decorator to each of your function. Check <a href="http://thecodeship.com/patterns/guide-to-python-function-decorators/" rel="nofollow">A guide to Python's function decorators</a>. For example, your decorator should be like:</p>

<pre><code>def wrap_error(func):
    def func_wrapper(*args, **kwargs):
        try:
           return func(*args, **kwargs)
        except:
           pass
    return func_wrapper
</code></pre>

<p>Now add this decorator with your function definition as:</p>

<pre><code>@wrap_error
def function1():
    some code
</code></pre>

<p>Functions having this decorator added to them won't raise any <code>Exception</code></p>

python python-2.7