[Tutor] User defined exceptions

Alan Gauld alan.gauld at yahoo.co.uk
Fri Oct 15 07:43:04 EDT 2021


On 15/10/2021 08:03, Manprit Singh wrote:


>     if score < 0 or score > 100:
>         raise MarksError("Invalid Score")    # Argument to raise is an

One other oint I meant to make is that it would be better to be more
specific in the message that is passed. tell the user what was wrong,
and how to put it right, not just that it was invalid.

For example:

raise MarksError("Score must be between 0 and 100")

tells the user what kind of score they should use.
You could go further and include a format string that inserts
the actual value received. That would also speed up debugging:

raise MarksError("Score of %d does not lie between 0 and 100" % score)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list