Exceptions: Raising, Catching and Recovering · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.

Raising and catching failures

Computing · Programming · ages 17-18
Name ______________________   Date ____________
  1. int('nope') raises ValueError. Which plan catches that exception around the conversion?

    • Put except ValueError before the call, without a try block
    • Put the call in try, with except ValueError below it
    • Put the call in try, but use except IndexError below it
  2. The path absent.txt does not exist. Your program calls open('absent.txt', 'r'). Which exception does this missing read path cause?

    • FileNotFoundError for the missing read path
    • ValueError for an invalid integer conversion
    • IndexError for an out-of-range list position
  3. In a plain Python script using default error reporting, an exception that remains uncaught prints an error and stops that script.

    Circle one:   True   False

  4. open('roster.txt', 'r') fails because the path is absent; request another path. int('fern') fails because its text is not an integer; reject the record. Which handlers match these policies?

    • FileNotFoundError requests a path; ValueError rejects the record
    • ValueError requests a path; FileNotFoundError rejects the record
    • FileNotFoundError handles both operations and always retries
  5. half_positive accepts only n > 0. It receives n = -8. Which action follows the taught input-rejection policy?

    • Return abs(n) / 2 as if the input were valid
    • Return 0 as a default without reporting the input
    • Raise ValueError to signal the unusable input
  6. number = 7 try: number = int('bad') except: pass print(number) What is the risk of this handler?

    • It replaces 7 with 0 because pass creates a default result
    • It reports stale 7 without signalling the failed conversion
    • It retries the conversion until valid numeric text appears
  7. A report must use actual file contents. Reading missing.csv raises FileNotFoundError. Which policy can recover without inventing contents?

    • Request another path and use contents only after a successful read
    • Treat the failed read as an empty file and claim the data was read
    • Use a contents variable even though this read never assigned it
  8. A handler catches ValueError with except Exception as err, prints err, then executes bare raise. What does raise do?

    • It retries the failed operation without leaving the handler
    • It continues after the handler as if no error had occurred
    • It propagates the current ValueError to the caller
LightMySky · lightmysky.comW1-mt_hEVa1JRrGu-s1

Answer key

For grown-ups. Fold this page away before handing over the rest.

Raising and catching failures W1-mt_hEVa1JRrGu-s1

  1. Put the call in try, with except ValueError below it · The risky conversion belongs inside try, and the following handler must match ValueError.
  2. FileNotFoundError for the missing read path · Read mode requires the path to exist. The absent path causes FileNotFoundError.
  3. True · Under the stated execution convention, an uncaught exception ends the script with error reporting.
  4. FileNotFoundError requests a path; ValueError rejects the record · The absent read path raises FileNotFoundError. The invalid integer text raises ValueError. Attach each stated policy to its matching handler.
  5. Raise ValueError to signal the unusable input · -8 violates n > 0. Raising ValueError reports that violation instead of silently changing the input or inventing a result.
  6. It reports stale 7 without signalling the failed conversion · The conversion fails before assignment. pass does nothing, so number remains 7 and the failure is silently suppressed.
  7. Request another path and use contents only after a successful read · A successful retry can provide actual contents. Treating the failed read as an empty file invents contents; using a variable this read never assigned does not establish valid data.
  8. It propagates the current ValueError to the caller · Bare raise inside the handler re-raises the current exception. Reporting it first does not suppress it.
Worksheet · LightMySky