'I am using try-and-except in a function. And other functions which can cause value errors, but it doesn't raise Exception. IDW?

Here's the code:

def TryExcept(foo):
    try:
        print('Using TryExcept')
        foo()
    except (ValueError, TypeError):
        print('Type or Value Error')

def add(a,b):
    print(f'Variables : {a} and {b}')
    print(int(a)+int(b))

TryExcept(add(1,2))
TryExcept(add('a','b'))


Solution 1:[1]

You can use functools.partial as an argument to your try/except function:

from functools import partial

TryExcept(partial(add, 1, 2))
TryExcept(partial(add, 'a', 'b'))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 a_guest