Summary

Exception handling is easier than the traditional approach of always checking the return type from functions. Exceptions can be class objects or string objects, although string object exceptions are no longer in favor. The newer, more correct way is to use class instances. In fact, as of Python version 1.5, all of the standard exceptions are class instances.

The try statement tries a block of code; if that block raises an exception, try either handles that exception or performs some type of cleanup. There are two forms of try. One has try, except, and else clauses. The except clause handles all exception classes derived from the class it mentions. The else clause is the default handler for code that must be executed if no exceptions are ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython 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.