6.10. Handling Errors

The problem of dealing with error conditions is as old as programming itself. Historically, dealing with errors was left to the programmer to worry about. That is, programming languages did not provide any special functionality for dealing with the error-handling problem.

The time-honored way to deal with errors involves two things:

  • Testing to see if an error has occurred.

  • Having decided that something has gone wrong, either (a) deal with it, or (b) return some signal to indicate that something has gone wrong.

Returning some form of error code gives other parts of the software a chance to deal with the error. This approach results in the following code pattern.

 CD-ROM reference=6040.txt def DoSomeThingClever: ... if x < ...

Get XML Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.