2

This question is about coding style.

Say I have a function, f. f depends on a condition C. It calls another function g as a subroutine. g also depends on condition C. If it finds condition C is not satisfied, it raises an exception. Now, in function f, should we write:

try:
    result = g()
except TheSpecificExcpetionGRaises:
    raise

or just plain old

g()

They both pass on the exception, but method one puts the traceback in the scope of function f, and method two puts the traceback in the scope of function g.

Note: the condition of C not being satisfied doesn't imply misuse of the function f. Or, in CS terms, the condition C is not part of the problem specification, rather we punt when condition C is false.

I don't know why I italicized everything and used math terms.

1 Answer 1

2

This applies to any language. If you don't plan on doing anything with the exception, or as a result of the exception being caught, then don't bother catching it. It saves you a few lines of code and indentation.

Seeing an except block could tell other programmers, who are busy and aren't reading every line of code, "Oh good. CrazyPython is handling this exception" and then not perform their own due diligence elsewhere.

If you can't handle the exception, let it blow sky-high. Someone else must handle it, because your code can't, and that's ok.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.