Learning Python - Mark Lutz [469]
Whether or not you will leverage the categories in the built-in class tree, it serves as a good example; by using similar techniques for class exceptions in your own code, you can provide exception sets that are flexible and easily modified.
Default Printing and State
Built-in exceptions also provide default print displays and state retention, which is often as much logic as user-defined classes require. Unless you redefine the constructors your classes inherit from them, any constructor arguments you pass to these classes are saved in the instance’s args tuple attribute and are automatically displayed when the instance is printed (an empty tuple and display string are used if no constructor arguments are passed).
This explains why arguments passed to built-in exception classes show up in error messages—any constructor arguments are attached to the instance and displayed when the instance is printed:
>>> raise IndexError # Same as IndexError(): no arguments
Traceback (most recent call last):
File " IndexError >>> raise IndexError('spam') # Constructor argument attached, printed Traceback (most recent call last): File " IndexError: spam >>> I = IndexError('spam') # Available in object attribute >>> I.args ('spam',) The same holds true for user-defined exceptions, because they inherit the constructor and display methods present in their built-in superclasses: >>> class E(Exception): pass ... >>> try: ... raise E('spam') ... except E as X: ... print(X, X.args) # Displays and saves constructor arguments ... spam ('spam',) >>> try: ... raise E('spam', 'eggs', 'ham') ... except E as X: ... print(X, X.args) ... ('spam', 'eggs', 'ham') ('spam', 'eggs', 'ham') Note that exception instance objects are not strings themselves, but use the __str__ operator overloading protocol we studied in Chapter 29 to provide display strings when printed; to concatenate with real strings, perform manual conversions: str(X) + "string". Although this automatic state and display support is useful by itself, for more specific display and state retention needs you can always redefine inherited methods such as __str__ and __init__ in Exception subclasses—the next section shows how. Custom Print Displays As we saw in the preceding section, by default, instances of class-based exceptions display whatever you passed to the class constructor when they are caught and printed: >>> class MyBad(Exception): pass ... >>> try: ... raise MyBad('Sorry--my mistake!') ... except MyBad as X: ... print(X) ... Sorry--my mistake! This inherited default display model is also used if the exception is displayed as part of an error message when the exception is not caught: >>> raise MyBad('Sorry--my mistake!') Traceback (most recent call last): File " __main__.MyBad: Sorry--my mistake! For many roles, this is sufficient. To provide a more custom display, though, you can define one of two string-representation overloading methods in your class (__repr__ or __str__) to return the string you want to display for your exception. The string the method returns will be displayed if the exception either is caught and printed or reaches the default handler: >>> class MyBad(Exception): ... def __str__(self): ... return 'Always look on the bright side of life...' ... >>> try: ... raise MyBad() ... except MyBad as X: ... print(X) ... Always look on the bright side of life... >>> raise MyBad() Traceback (most recent call last): File " __main__.MyBad: Always look on the bright