Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

VII. Exceptions and Tools > 34. Exception Objects

Chapter 34. Exception Objects

So far, I’ve been deliberately vague about what an exception actually is. As suggested in the prior chapter, in Python 2.6 and 3.0 both built-in and user-defined exceptions are identified by class instance objects. Although this means you must use object-oriented programming to define new exceptions in your programs, classes and OOP in general offer a number of benefits.

Here are some of the advantages of class-based exceptions:

  • They can be organized into categories. Exception classes support future changes by providing categories—adding new exceptions in the future won’t generally require changes in try statements.

  • They have attached state information. Exception classes provide a natural place for us to store context information for use in the try handler—they may have both attached state information and callable methods, accessible through instances.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


 Â