Monday, June 6, 2016

A quick try except in python


Please use try/except for cross service or real time user input validation, its safer to throw exception rather failing the whole program

# Program to convert all given values into int

# Ex. of try except

for v in 1,2,10,'a',4,5,10,'v',"abc",1:
        try:
                print "Vint:",int(v)
        except ValueError as ex:

                print "{} cannot be converted to int: {}".format(v,ex)


No comments:

Post a Comment