Catch the exception

Python spots when an exception is thrown, and you can write some code to run when the exception occurs. This is called catching the exception. The code that you run when there's an error resulting in the thrown exception is called an exception handler.

fileD = open("deliveries.txt", "a")

fileD.write( fileD.write( fileD.write( fileD.write( fileD.write( fileD.write( depot.set("") description.delete(0, description.delete(0, address.delete("1.0",

%s\n" % depot.get()) Description:\n") %s\n" % description.get()) Address :\n") s\n" % address.get("1.0", END))

Exception handling


Exception handling


Creating exception handlers can really make life easy for your users. Instead of a flaky program that crashes or fails silently the first time something weird happens, you can write programs that gracefully recover from errors. Exception handlers tidy up when something goes wrong and can even let your user know that something strange happened.

That's what you need here: an exception handler that tells the user when there's a problem writing a delivery to the file.

How are exception handlers written in Python?

A piece of code that runs when an exception is thrown is called an exception handler.

try to except

Was this article helpful?

0 0

Post a comment