Exception Underloading

ExceptionUnderloading is when the same exception signals situations that require distinct handling. Or in layman terms, the exception object isn't loaded with sufficient information for any catch block to deal with the situation.

Catching an underloaded exception without propagation is a bug.

In particular, it is a problem catching an underloading exception even when it can be proven that it currently can only have been raised to signal the situation that is being handled; it may be raised to signal a different situation in the future. Here follows an example:

 def prompt_for_age():
     # int() raises ValueError if its argument cannot be converted to an integer.
     return int(user_input('what is your age? '))

def main(): while True: try: print('your age is %d' % prompt_for_age()) break except ValueError?: print('invalid input, try again.')

The problem arises when `user_input()` in the future is changed to raise ValueError. Perhaps this is done as a programming error, but had we not cought the ValueError here, such a programming error would have been properly reported to the user.

My recommended solution is ConvertExceptions. I am interested in hearing about alternative solutions.

-- RobertEngdahl


CategoryException


EditText of this page (last edited July 23, 2014) or FindPage with title or text search