[Tutor] FW: query

Alan Gauld alan.gauld at btinternet.com
Wed Jun 24 18:27:36 CEST 2015


On 24/06/15 13:58, abhijeet560 at yahoo.in wrote:
> Hey guys can anybody tell me what's wrong with this code: The code is below?

Please in future
1) start a new thread with a new post, do not hijack somebody else's 
query. It messes up the archive and threaded mail/newsreaders

2) Use plain text for posting code, your post is all messed up by the 
mail system so we can't see the code clearly. It is all on one line...

> Actually the point is that when we put "34h4" type of value
 > it's an valueerror but here no handling is been performed

The handling only happens if it occurs inside a try block. It looks as 
if your type conversion (int(...)) happens outside the try block.
The error is raised by the type conversion.

> while 1:    number=int(input("Enter the number which u want to check for odd and even :"))    try :        if number%2==0:            print("The number",number ," is Even")        else:            print("The number ",number ," is Odd")              except  ValueError:        print("Invalid Input")

Finally, handling an error by simply printing a bland error message
is usually not a good idea. You effectively hide a lot of valuable 
debugging information. You would be better to just let Python print
out its usual, much more helpful, error message.

(The exception is where it's the top level of an end-user program
where the Python trace might scare the users. But that should only
be after you have thoroughly debugged it and handled most of the
likely problem scenarios, and hopefully logged the error data
into a logfile or sent it as an email to your support desk.)

-- 
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