#exception handling example # Read the file record by record # and calculate the total number of birds observed fileReadError = False birdFile = open("birdFile", "r") totalBirdsSeen=0 while fileReadError == False: try: birdRec = birdFile.readline() except: print("Attempt to read beyond end of file") print ("Number of records in file = ", n-1) fileReadError = True print ("FileReadError ",fileReadError) else: field = birdRec.split(",") try: birdsSeen = int(field[1]) except: birdsSeen = 0 print ("Read beyond end of file") fileReadError = True print ("FileReadError ",fileReadError) else: totalBirdsSeen = totalBirdsSeen + birdsSeen print("Birds seen ",birdsSeen) print("Total birds seen ", totalBirdsSeen) try: birdFile = open("nonexistentfile","r") except: print("Sorry, can't find this file") #Test user input errors print("testing for bad data input") badInteger = input("enter integer: ") try: goodint = int(badInteger) except: print("that is not an integer") else: print ("You entered ", goodint)