Objective
Understanding the order in which try, except, else and finally run is key: finally always runs, even when the try has already done return.
Instructions
- Define
process(mode, log)that appends to a list each block that runs (try,except,else,finally). Depending on the mode:okends without error;failraisesValueError(caught);returndoesreturn "R"inside thetry;boomraisesKeyError(not caught). It returns"OK","E","R"or, after theKeyError, returns nothing. - The first line has the number
nof modes; each one is a line. Show the log separated by spaces,->and the returned value (orpropagatedif the error propagated and the main program catches it).
4
ok
fail
return
boom
try else finally -> OK
try except finally -> E
try finally -> R
try finally -> propagated
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise