Page 335 -
P. 335

exception handler


                        Exception Magnets Solution

                        You were asked to assemble the code to handle an exception in the
                        save_data() function. The exception handler needs to display
                        the details of the exception in the title bar of the window. You
                        needed to remember to indent the code in the exception handler, in
                        addition to the code in the try block.








                      def save_data():
                            try:

                                      fileD = open("deliveries.txt", "a")
                                      fileD.write("Depot:\n")
                                      fileD.write("%s\n" % depot.get())
                                      fileD.write("Description:\n")
                                      fileD.write("%s\n" % description.get())
                                      fileD.write("Address:\n")
                                      fileD.write("%s\n" % address.get("1.0", END))
                                      depot.set("")
                                      description.delete(0, END)
                                      description.delete(0, END)
                                                                      We’re using format strings to
                                      address.delete("1.0", END)      display the exception in the title.

                            except    Exception     as    ex:

             This is the exception   app.title(  "Can’t write to the file %s"   %    ex   )
             handler code.

                                             The exception handler can have
                                              several lines as long as they are
                                             all indented in the same way.














           300    Chapter 8 ½
   330   331   332   333   334   335   336   337   338   339   340