12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

In practice, try/except combinations are useful for catching and recovering fromexceptions, and try/finally combinations come in handy to guarantee that terminationactions will fire regardless of any exceptions that may occur in the try block’scode. For instance, you might use try/except to catch errors raised by code that youimport from a third-party library, and try/finally to ensure that calls to close files orterminate server connections are always run. We’ll see some such practical exampleslater in this part of the book.Although they serve conceptually distinct purposes, as of <strong>Python</strong> 2.5, we can nowmix except and finally clauses in the same try statement—the finally is run on theway out regardless of whether an exception was raised, and regardless of whether theexception was caught by an except clause.That is the majority of the exception story; exceptions really are a simple tool. In therest of this part, we’ll fill in some of the details about the statements involved, examinethe other sorts of clauses that can appear under a try, and discuss string- andclass-based exception objects.<strong>Python</strong> exceptions are a high-level control flow device. They may be raised by<strong>Python</strong>, or by your own programs; in both cases, they may be ignored (to trigger thedefault error message), or caught by try statements (to be processed by your code).The try statement comes in two logical formats that, as of <strong>Python</strong> 2.5, can be combined—onethat handles exceptions, and one that executes finalization code whetherexceptions occur or not. <strong>Python</strong>’s raise and assert statements trigger exceptions ondemand. With that overview in mind, let’s take a deeper look at these statements’general forms.The try/except/else StatementIn the following discussion, I’ll first present try/except/else and try/finally as separatestatements because they serve distinct roles and cannot be combined in versionsof <strong>Python</strong> prior to 2.5. As you’ve seen, in <strong>Python</strong> 2.5 except and finally can bemixed in a single try statement; I’ll explain the implications of this change afterwe’ve explored the two original forms in isolation.try is a compound statement; its most complete form is sketched below. It startswith a try header line, followed by a block of (usually) indented statements, thenone or more except clauses that identify exceptions to be caught, and an optionalelse clause at the end. The words try, except, and else are associated by indentingthem to the same level (i.e., lining them up vertically). For reference, here’s the generalformat:try:except :# Run this action first# Run if name1 is raised during try blockThe try/except/else Statement | 581

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!