Should exceptions have an __init__ method?

Graham Ashton gashton at cmedltd.com
Fri Apr 19 10:37:14 EDT 2002


I've got a quick query about defining exceptions. In Beazley's Essential
Reference book (2nd ed.), on page 59 it says that you should define your
exceptions with their own __init__ method that accepts an args argument,
if you want the values that you pass into them to be handled correctly
in "tracebacks and other diagnostics" (see class F below for an
example).

I'm unable to determine the difference between these two exception
classes though (where class E is essentially empty):

Python 2.2 (#2, Mar 11 2002, 13:24:00) 
[GCC 2.95.3 20010315 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class E(Exception):
...     pass
... 
>>> raise E, "foobar"
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
__main__.E: foobar
>>> class F(Exception):
...     def __init__(self, args=None):
...         self.args = args
... 
>>> raise F, "foobar"
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
__main__.F: foobar


As you can see, I'm on 2.2, and Beazley wrote his book for 2.1. Has 2.2
made subclasses of Exception more intelligent, or am I missing
something?

Cheers.

-- 
Graham Ashton






More information about the Python-list mailing list