handle multiple exceptions : Exception Handle « Exception « Python






handle multiple exceptions

handle multiple exceptions
 


# 
print
for value in (None, "Hi!"):
    try:
        print "Attempting to convert", value, "-->",
        print float(value)
    except(TypeError, ValueError):
        print "Something went wrong!"

print
for value in (None, "Hi!"):
    try:
        print "Attempting to convert", value, "-->",
        print float(value)
    except(TypeError):
        print "I can only convert a string or a number!"
    except(ValueError):
        print "I can only convert a string of digits!"


           
         
  








Related examples in the same category

1.Simple exception handling exampleSimple exception handling example
2.Catch exception in a functionCatch exception in a function
3.Handling Exceptions
4.Demonstrates handling exceptionsDemonstrates handling exceptions
5.Exception handlers handle Exceptions occur inside functions. Exception handlers handle Exceptions occur inside functions.