Nested try statements with finally

The Python program below processes a list of tokens with a try statement nested inside another. Trace it to the end and give the three integers it prints: the final values of total, handled and cleanups.


Python
total = 0
handled = 0
cleanups = 0
try:
    for token in ["5", "2.5", "x", "0", "8"]:
        try:
            total += 10 // int(token)
        except ValueError:
            handled += 1
        finally:
            cleanups += 1
except ZeroDivisionError:
    total = total + 100
print(total, handled, cleanups)
Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More try/except Blocks Questions: