Let's say I have the following four variables:
>>> f1 = (print, 1, 2 ,3 ,4) >>> f2 = (exit, 1, 2 ,3 ,4) >>> f3 = (1, 2, 3, 4) >>> f4 = 4
In a hypothetical program, I expect that each of these variables will contain a tuple, the first element of which should be the name of the function, and the subsequent elements should be specified as parameters of the function in order.
I could call a function stored this way:
>>> f1[0](*f1[1:]) 1 2 3 4
However, most of these variables are not in an excluded format, and I would like to be able to encapsulate their call inside try / except blocks to handle these situations.
Now, even though function calls f2 , f3 and f4 break for a radically different reason, they all throw the same exception, TypeError :
>>> f2[0](*f2[1:]) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: __call__() takes from 1 to 2 positional arguments but 5 were given >>> f3[0](*f3[1:]) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not callable >>> f4[0](*f4[1:]) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not subscriptable
So, to do the general:
try: f[0](*f[1:]) except TypeError: # handle exception
Would not provide me with enough information to handle each exception.
What would be the proper way to distinguish between separate exceptions of the same type in Python?