Follow an exception through two finally blocks

The Python 3 program below validates a stock adjustment. Arrange the lines it prints in the order they appear in the output when it runs.


Python
def check(qty):
    try:
        print("validate")
        if qty < 0:
            raise ValueError("negative quantity")
        print("accepted")
    finally:
        print("release lock")

def process(qty):
    try:
        check(qty)
        print("update stock")
    except ValueError as err:
        print("log:", err)
    finally:
        print("close file")
    print("return to menu")

process(-3)
  1. 1.

    close file

  2. 2.

    release lock

  3. 3.

    validate

  4. 4.

    return to menu

  5. 5.

    log: negative quantity

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 Exceptions and Assertions Questions: