very newbie question about exception handling

L

Louis.Soninhu

code sample:
----------------------------------------------
i=input()
try:
x=int(i)
print "you input an integer"
except ValueError:
print "you must input an integer"
------------------------------------------------
when I input a value like, b
I got the traceback message instead of proper exception handling:

NameError: b is not defined

I also tried
 
R

Ricardo Aráoz

code sample:
----------------------------------------------
i=input()
try:
x=int(i)
print "you input an integer"
except ValueError:
print "you must input an integer"
------------------------------------------------
when I input a value like, b
I got the traceback message instead of proper exception handling:

NameError: b is not defined

I also tried
------------------------------------------
try: x=int(i)
except ValueError: print "something"
except NameError: pting "something"
else: print "something"
----------------------------------------------------
It still couldn't capture the exception, just the same traceback error
msg.

I'm really confused, any suggestions please?
Yes, you must use raw_input() (check the manual on "input()").
 
D

Dennis Lee Bieber

code sample:
----------------------------------------------
i=input()
try:
x=int(i)
print "you input an integer"
except ValueError:
print "you must input an integer"
------------------------------------------------
when I input a value like, b
I got the traceback message instead of proper exception handling:

NameError: b is not defined
And did you look at the traceback to see which LINE raised the
exception...

Then look at the manual for the difference between input() and
raw_input().
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top