we encountered examples in this category in as summarythoughexception processing tools are also commonly used to ensure that system resources are finalizedregardless of whether an error occurs during processing or not for examplesome servers require connections to be closed in order to terminate session similarlyoutput files may require close calls to flush their buffers to disk for waiting consumersand input files may consume file descriptors if not closedalthough file objects are automatically closed when garbage-collected if still openin some pythons it may be difficult to be sure when that will occur as we saw in the most general and explicit way to guarantee termination actions for specific block of code is the try/finally statementmyfile open( ' :\code\textdata'' 'tryprocess myfile finallymyfile close(as we also sawsome objects make this potentially easier in python and later by providing context managers that terminate or close the objects for us automatically when run by the with/as statementwith open( ' :\code\textdata'' 'as myfileprocess myfile so which option is better hereas usualit depends on your programs compared to the traditional try/finallycontext managers are more implicitwhich runs contrary to python' general design philosophy context managers are also arguably less general --they are available only for select objectsand writing user-defined context managers to handle general termination requirements is more complex than coding try/finally on the other handusing existing context managers requires less code than using tryfinallyas shown by the preceding examples moreoverthe context manager protocol supports entry actions in addition to exit actions in factit can save line of code when no exceptions are expected at all (albeit at the expense of further nesting and indenting file processing logic)myfile open(filename' 'process myfile myfile close(traditional form with open(filenameas myfileprocess myfile context manager form stillthe implicit exception processing of with makes it more directly comparable to the explicit exception handling of try/finally although try/finally is the more widely applicable techniquecontext managers may be preferable where they are already availableor where their extra complexity is warranted designing with exceptions