Home > Enterprise >  Handling exception in main function interface
Handling exception in main function interface

Time:09-22

I have a main function that takes a list argument and proceeds with every element in the list. It is not necessary for me to stop the program whenever one element raises an exception. The problem is that the input list is passed through different functions and it is a pain to handle exceptions for all those functions.

Is there any way to continue to proceed with the next element when the current element in the list raises an exception?

Some pseudo-code:

def main(list_) -> None:   
    func1(list_)
    func2(list_)
    func3(list_)

Expected behavior:

def main(list_) -> None:   
    try:
        func1(list_)
        func2(list_)
        func3(list_)
    except:
        continue with the next element
        print(Element i raise Exception..., Skipping i...)

CodePudding user response:

With a “reversed” list of the functions, this while-loop might be suitable for the body of the main function:

funcs = [funcN, funcN-1, …, func2, func1]
while any(funcs):
    try: funcs.pop()(list_)
    except: continue

CodePudding user response:

Each function needs to be in its own try block.

def main(list_) -> None:
    for f in (func1, func2, func3):
        try:
            f(list_)
        except Exception as e:
            print(e)

CodePudding user response:

If all your functions has the same input list_, the other answers should be enough and are preferable due to simplicity. But in case the arguments vary, you might want to wrap the call to those functions inside a decorator in where you would catch the errors:

Style 1

def catch_exc(func, *args, **kwargs):
    try:
        func(*args, **kwargs)
    except Exception as error:
        print(func, "Element i raise Exception..., Skipping i...", error)
    else:
        print(func, "Success")

def func1(list_): raise Exception("Anything")
def func2(list_): raise Exception("Something")
def func3(list_): return "Success"
def func4(list_): raise Exception("Whatever")

def main(list_) -> None:
    catch_exc(func1, list_)
    catch_exc(func2, list_)
    catch_exc(func3, list_)
    catch_exc(func4, list_)

main([1,2,3,4])

Style 2

def catch_exc(func):
    def wrapper(*args, **kwargs):  # Ideally decorate this with <functools.wraps(func)>
        try:
            func(*args, **kwargs)
        except Exception as error:
            print(func, "Element i raise Exception..., Skipping i...", error)
        else:
            print(func, "Success")
    return wrapper

@catch_exc
def func1(list_): raise Exception("Anything")
@catch_exc
def func2(list_): raise Exception("Something")
@catch_exc
def func3(list_): return "Success"
@catch_exc
def func4(list_): raise Exception("Whatever")

def main(list_) -> None:
    func1(list_)
    func2(list_)
    func3(list_)
    func4(list_)

main([1,2,3,4])

Output

<function func1 at 0x149f3d911c10> Element i raise Exception..., Skipping i... Anything
<function func2 at 0x149f3d911ee0> Element i raise Exception..., Skipping i... Something
<function func3 at 0x149f3d911f70> Success
<function func4 at 0x149f3d91b040> Element i raise Exception..., Skipping i... Whatever
  • Related