this cuts the code down to ten linesor including defining the exceptionand is much easier to read if the item is found we raise our custom exception and the except block' suite is executed--and the else block is skipped and if the item is not foundno exception is raised and so the else suite is executed at the end let' look at another example to see some of the different ways that exceptionhandling can be done all of the snippets are taken from the checktags py programa program that reads all the html files it is given on the command line and performs some simple tests to verify that tags begin with "<and end with ">"and that entities are correctly formed the program defines four custom exceptionsclass invalidentityerror(exception)pass class invalidnumericentityerror(invalidentityerror)pass class invalidalphaentityerror(invalidentityerror)pass class invalidtagcontenterror(exception)pass the second and third exceptions inherit from the firstwe will see why this is useful when we discuss the code that uses the exceptions the parse(function that uses the exceptions is more than lines longso we will show only those parts that are relevant to exception-handling fh none tryfh open(filenameencoding="utf "errors false for linoline in enumerate(fhstart= )for columnc in enumerate(linestart= )trythe code begins conventionally enoughsetting the file object to none and putting all the file handling in try block the program reads the file line by line and reads each line character by character notice that we have two try blocksthe outer one is used to handle file object exceptionsand the inner one is used to handle parsing exceptions elif state =parsing_entityif =";"if entity startswith("#")if frozenset(entity[ :]hexdigitsraise invalidnumericentityerror(elif not entity isalpha()raise invalidalphaentityerror(