[Tutor] Working with error messages

Andre Engels andreengels at gmail.com
Sun May 6 16:40:07 CEST 2007

2007/5/6, Alan Gilfoy <agilfoy at frontiernet.net>:
> I have a number-to-Roman numeral program that churns out ValueError
> messages with a few improper input cases:
> 1. not an integer
> 2. larger than 3999
> 3. smaller than 0
> When I run the program via IDLE, and I give one of these improper
> inputs, the interpreter closes down the program and then displays the
> appropriate ValueError message.
> I would like the appropriate ValueError message to be displayed before
> the program shuts down, or at least a generic ValueError message.
> Is looking at my specific pieces of code necessary to help with this?
> I want this because if the program's being run as an application, the
> application window closes down as soon as the program closes, and the
> user doesn't get to see the message. [When I doubleclick on the .py
> file in Windows Explorer, it runs as a .exe, for example.]

You could try something like:

   <Your program>
except ValueError:
   raw_input("I got a ValueError. Press enter to end the program.")

Of course, the more appropriate thing to do would be to catch the
error at a lower level than the whole program - check where you get
the ValueError, put that line in a try, and take an appropriate action
in the except.

Andre Engels, andreengels at gmail.com
ICQ: 6260644  --  Skype: a_engels

More information about the Tutor mailing list