[Tutor] Nicer error message

Steven D'Aprano steve at pearwood.info
Tue May 31 17:02:16 CEST 2011


Válas Péter wrote:
> Hi,
> 
> my code is (Python 2.5):
> 
> class SomeError(Error):
>   """Uff!"""
> raise SomeError, "blahblah"
> 
> Screen result:
> __main__.SomeError: "blahblah"


I get

Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
NameError: name 'Error' is not defined



> How can I make "__main__" disappear from output?
> Thanks

You should inherit exceptions from the Exception class:


class SomeError(Exception):
     pass


If you put this in a module, then your exception will print:


<name of the module>.SomeError: "blahblah"


which is exactly what you need when debugging. Only built-in exceptions 
don't print the module name.

If you don't put it in a module, but just enter it at the interactive 
interpreter, it goes into a special module called "__main__". You just 
have to live with it.



-- 
Steven



More information about the Tutor mailing list