elif state =parsing_entityraise eoferror("missing ';at end of filenameat the end of parsing filewe need to check to see whether we have been left in the middle of an entity if we havewe raise an eoferrorthe built-in end-of-file exceptionbut give it our own message text we could just as easily have raised custom exception except (invalidentityerrorinvalidtagcontenterror)pass already handled except eoferror as errprint("error unexpected eof:"errexcept environmenterror as errprint(errfinallyif fh is not nonefh close(for the outer try block we have used separate except blocks since the behavior we want varies if we have parsing exceptionwe know that an error message has already been output and the purpose is simply to break out of reading the file and to move on to the next fileso we don' need to do anything in the exception handler if we get an eoferror it could be caused by genuine premature end of file or it could be the result of us raising the exception ourselves in either casewe print an error messageand the exception' text if an environmenterror occurs ( if an ioerror or an oserror occurs)we simply print its message and finallyno matter whatif the file was openedwe close it custom functions ||functions are means by which we can package up and parameterize functionality four kinds of functions can be created in pythonglobal functionslocal functionslambda functionsand methods every function we have created so far has been global function global objects (including functionsare accessible to any code in the same module ( the same py filein which the object is created global objects can also be accessed from other modulesas we will see in the next local functions (also called nested functionsare functions that are defined inside other functions these functions are visible only to the function where they are definedthey are especially useful for creating small helper functions that have no use elsewhere we first show them in