[Tutor] Nicer error message

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


Válas Péter wrote:
> 2011. május 31. 16:39 Japhy Bartlett írta, <japhy at pearachute.com>:
> 
>> You could write directly to sys.stderr instead of raising an error.
>>
>> If other programmers have to work with your code, they'll probably
>> find this _incredibly_ annoying.
>>
> 
> You mean there is no way to write it nicely?
> Or are error messages not intended to be written on screen?
> Yes, this wil be used by others, and I want it be nice.  (Although they may
> avoid making errors if they find it annoying. :-PP)


Tracebacks are intended to be *useful*, not "nice". Nobody ever said, 
"Oh look, what a pretty error message! I think I'll take a screen shot 
and set it as my desktop wallpaper!"

*wink*

The module is part of the fully qualified name of the exception class. 
It's a useful thing to display.

When you get a traceback printed, if you saw this:

Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   [...more info here...]
SomeErrorIveNeverHeardOf: "message"


you know where the error occurred, but you don't know where the 
exception itself comes from, because the module it is defined in isn't 
shown. By showing the module, you now can track down the source of the 
exception itself, as well as the line of code that raised the exception.

In the worst case, the line of code might look something like this:


raise get_some_exception(x, y, z)("error message")


where get_some_exception is a function that returns an exception class. 
It might return exceptions with the same name but from different 
modules: A.Error, B.Error, C.Error, ...

Believe me, there will come a day that you want to know where the 
exception class itself came from, and then you'll be glad the module is 
shown.



-- 
Steven



More information about the Tutor mailing list