Proper way to handle errors in a module

Tycho Andersen tycho at tycho.ws
Thu May 12 16:26:27 EDT 2011


On Thu, May 12, 2011 at 03:12:39PM -0500, Andrew Berg wrote:
> On 2011.05.12 02:25 PM, MRAB wrote:
> > You can raise an exception wherever you like! :-)
> If I raise an exception that isn't a built-in exception, I get something
> like "NameError: name 'HelloError' is not defined". I don't know how to
> define the exception.

You'll have to define it, as you would anything else (exceptions are
just regular "things"; in fact you can raise anything that's a class
or instance). I typically don't put a whole lot in my exception
classes, though.

point:~/working$ python
Python 2.6.2 (r262:71600, Jun  8 2009, 11:11:42) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class HelloError(Exception): pass
... 
>>> raise HelloError("hello!")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  __main__.HelloError: hello!

\t



More information about the Python-list mailing list