stuff(filefinallyfile close(print('not reached'raises exception always close file to flush output buffers continue here only if no exception when the function in this code raises its exceptionthe control flow jumps back and runs the finally block to close the file the exception is then propagated on to either another try or the default top-level handlerwhich prints the standard error message and shuts down the program hencethe statement after this try is never reached if the function here did not raise an exceptionthe program would still execute the finally block to close the filebut it would then continue below the entire try statement in this specific casewe've wrapped call to file-processing function in try with finally clause to make sure that the file is always closedand thus finalizedwhether the function triggers an exception or not this waylater code can be sure that the file' output buffer' content has been flushed from memory to disk similar code structure can guarantee that server connections are closedand so on as we learned in file objects are automatically closed on garbage collection in standard python (cpython)this is especially useful for temporary files that we don' assign to variables howeverit' not always easy to predict when garbage collection will occurespecially in larger programs or alternative python implementations with differing garbage collection policies ( jythonpypythe try statement makes file closes more explicit and predictable and pertains to specific block of code it ensures that the file will be closed on block exitregardless of whether an exception occurs or not this particular example' function isn' all that useful (it just raises an exception)but wrapping calls in try/finally statements is good way to ensure that your closing-time termination activities always run againpython always runs the code in your finally blocksregardless of whether an exception happens in the try block notice how the user-defined exception here is again defined with class--as we'll see more formally in the next exceptions today must all be class instances in and later releases in both lines unified try/except/finally in all versions of python prior to release (for its first years of lifemore or less)the try statement came in two flavors and was really two separate statements--we could either use finally to ensure that cleanup code was always runor write unless python crashes completelyof course it does good job of avoiding thisthoughby checking all possible errors as program runs when program does crash hardit is usually due to bug in linkedin extension codeoutside of python' scope exception coding details