Let's say I have a methodA that raises an exception:
def methodA(x, y):
if y != 0:
z = x / y
return z
else:
raise ZeroDivisionError("zero can not be a denominator")
And in methodB I called methodA without handling the exception:
def methodB(x, y):
print methodA(x, y)
Here I want something that could warn me there is a potential risk of ZeroDivisionError
in methodB, and that it's better to catch it.
Is there any way to add some certain codes in methodA, or can we use some tool to find that I ignored some important exceptions?
I'm not aware of anything close to such a mechanism in Python, but in general methodA
should not care about if methodB
deals with the exception or not. At each level (such as methodB
) only exceptions that methodB
could deal with (at some documented/specified level) should be caught. If methodB
can not deal with for example ZeroDivisionError
then perhaps nobody can (and then not catching it is the correct hing to do), or, it could be that in some call graph, methodC
will catch it and do what is correct in context of methodC
.
Also note the somewhat related (but reverse) question for Java: Java: checked vs unchecked exception explanation
and for C++ https://softwareengineering.stackexchange.com/questions/114338/why-are-exception-specifications-bad and Should I use an exception specifier in C++?