[Python-ideas] Having a messageless exception show its docstring

Michael Foord fuzzyman at gmail.com
Sat Jan 9 13:35:10 CET 2010


2010/1/9 cool-RR <cool-rr at cool-rr.com>

> I propose that when raising an exception without providing a message, the
> first line of the docstring will be shown. (I mean shown in the traceback
> where the message would normally be.
>
> Example session:
>
> ####################################
>
> >>> class MyExcpetion(Exception):
> '''Trying to fit a square piece into a round hole.'''
>
>
class MyException(Exception):
     "Some docstring"
    def __init__(self, msg=None):
         if msg == None:
             msg = self.__doc__
         Exception.__init__(self, msg)

All the best,

Michael



> >>> raise MyExcpetion('Problem bla bla')
>
> Traceback (most recent call last):
>   File "<pyshell#3>", line 1, in <module>
>     raise MyExcpetion('Problem bla bla')
> MyExcpetion: Problem bla bla
>
> >>> raise MyExcpetion()
>
> Traceback (most recent call last):
>   File "<pyshell#4>", line 1, in <module>
>     raise MyExcpetion()
> MyExcpetion: Trying to fit a square piece into a round hole.
>
> ####################################
>
> This will be useful for me because I often raise exceptions and have
> nothing to say besides repeating the exception's docstring, and I have many
> places in the code where I'd want to conditionally raise it. I'm faced with
> two choices: Type the exception's docstring in every place, which is lame,
> and breaks DRY. Or just raise the exception without any message, which may
> potentially confuse users.
>
> This is the motivation for this suggestion, so I could be succinct without
> confusing users.
>
> What do you think?
>
> Ram.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
>


-- 
http://www.ironpythoninaction.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100109/a1fcd0be/attachment.html>


More information about the Python-ideas mailing list