Watch for exceptions with tryexcept

In order to recover from an error as it happens, you need to indicate the code that might throw an exception. In Python, you do this with try and except.

All you need to do is take the piece of potentially troublesome code and add the try and except labels:

Put the exception handler labels around all • of this Code.

If an exception is thrown

ANYWHERE in this----

section, the computer will jump to the code HERE.

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) address.delete("1.0", END)

Note: -this Code •s indented "»de»- the "try" statement except Exception as ex:

This is where the EXCEPTION -> HANDLER code goes.

This is where the EXCEPTION -> HANDLER code goes.

■ Inside the handler, the exception is assigned to a variable called "ex".

If an exception is thrown between the try and except labels, the code that follows the except label runs. The code that threw the exception is abandoned. If no exception occurs, the code runs normally and the code that comes after the except label is ignored.

Notice that the try/except labels are wrapped around all of the function's code. If there's a problem opening the deliveries . txt file, you don't ever want to try writing to it. So, when trouble strikes, you should adandon ship and skip to the code that tries to recover from the error.

The code that then runs is the exception handler.

Exception Magnets

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. Note: remember to indent the code in the exception handler, in addition to the code in the try block.

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) address.delete("1.0", END)

exception handler

A Exception Magnets Solution s

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 def save data 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)

arirlrpqt; Helefel"! fl" EHDl

This is the exception ■ al handler Code.

except

This is the exception ■ al except

The exception handler can have several lines as long as they are a|1 mdented in the same way.

ISST Drivq

Let's see if the code works. Make sure the deliveries . txt file is readonly. Then run the new version of the program in IDLE and try to record a delivery by clicking on the Save button.

Clicking "Save" causes the title bar to change, due to the error.

Note: make sure deliveries.txt is set to read-only.

deliveries.txt

Note: make sure deliveries.txt is set to read-only.

deliveries.txt

Ciir't write to the file [Errno 13) Permission denied: 'deliveries.txt' + - D

Depot: Dallas, TX

Description

Stinky Cheese

Address:

Stinka-rVUs 47 WlbH I H|]iiinj âv it ft] n [Jallas, iX UM

Save

Depot: Dallas, TX

Description

Stinky Cheese

Address:

Save

1 \ \

Sure enough, when you try to save the delivery details, the program catches the exception and the exception handler displays the error message in the window title.

Wonder what the people at Head-Ex will think of this?

unexceptional exception handler

Was this article helpful?

0 0

Post a comment