we set the file objectfhto none because it is possible that the open(call will failin which case nothing will be assigned to fh (so it will stay as none)and an exception will be raised if one of the exceptions we have specified occurs (ioerror or oserror)after printing the error message we return an empty list but note that before returningthe finally block' suite will be executedso the file will be safely closed--if it had been successfully opened in the first place notice also that if an encoding error occurseven though we don' catch the relevant exception (unicodedecodeerror)the file will still be safely closed in such cases the finally block' suite is executed and then the exception is passed up the call stack--there is no return value since the function finishes as result of the unhandled exception and in this casesince there is no suitable except block to catch encoding error exceptionsthe program will terminate and print traceback dealing with runtime errors we could have written the except clause slightly less verboselyexcept environmenterror as errprint(errreturn [this works because environmenterror is the base class for both ioerror and oserror in we will show slightly more compact idiom for ensuring that files are safely closedthat does not require finally block context managers raising exceptions exceptions provide useful means of changing the flow of control we can take advantage of this either by using the built-in exceptionsor by creating our ownraising either kind when we want to there are three syntaxes for raising exceptionsraise exception(argsraise exception(argsfrom original_exception raise when the first syntax is used the exception that is specified should be either one of the built-in exceptionsor custom exception that is derived from exception if we give the exception some text as its argumentthis text will be output if the exception is printed when it is caught the second syntax is variation of the first--the exception is raised as chained exception (covered in that includes the original_exception exceptionso this syntax is used inside except suites when the third syntax is usedthat iswhen no exception is specifiedraise will reraise the currently active exception--and if there isn' one it will raise typeerror chained exceptions