Book Image

Python Essentials

By : Steven F. Lott
Book Image

Python Essentials

By: Steven F. Lott

Overview of this book

Table of Contents (22 chapters)
Python Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Summary


In this chapter, we've seen how we can use Python exceptions to write programs which properly handle unexpected conditions. The various kinds of exceptions reflect external conditions as well as internal conditions that may alter how our program behaves. We can use exception clauses to implement fallback processing so that our program handles these exceptional conditions gracefully.

We've also seen some things which are discouraged. The empty except clause—which matches too many kinds of exception classes—is something which is legal but should not be used.

The idea of Look Before You Leap (LBYL) programming is also generally discouraged. The Pythonic approach is summarized as Easier to Ask Forgiveness than to ask Permission (EAFP). The general approach is to wrap operations in a try statement and write appropriate exception handlers for the meaningful exceptions.

Some exceptions, such as RuntimeError or SyntaxError, should not be handled by ordinary application programming. These exceptions...