else and finally

A try statement has two more optional parts:

  • else runs only if the try block raised nothing. Put follow-up work here, so the try block stays small and its except can't catch errors from the follow-up.
  • finally always runs: after success, after a caught error, after an uncaught one, and even after return. Use it for cleanup like closing a connection.

The order is try, except, else, finally.

The surprise is in the output: done with prints before the returned value. finally runs on the way out of the function, before the caller gets the result.

Never return inside finally. It throws away any exception in flight, and Python 3.14 warns about it.

Run it, then add "5" to the inputs. Which part raises, and which clause catches it?