Simple exception handling example : Exception Handle « Exception « Python

Simple exception handling example

Simple exception handling example

number1 = raw_input( "Enter numerator: " )
number2 = raw_input( "Enter denominator: " )

   number1 = float( number1 )
   number2 = float( number2 )
   result = number1 / number2
# float raises a ValueError exception   
except ValueError:
   print "You must enter two numbers"
except ZeroDivisionError:
   print "Attempted to divide by zero"
   print "%.3f / %.3f = %.3f" % ( number1, number2, result )


Related examples in the same category

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