Demonstrating a programmer-defined exception class.
import math
class NegativeNumberError( ArithmeticError ): """Attempted improper operation on negative number.""" pass
def squareRoot( number ): """Computes square root of number. Raises NegativeNumberError if number is less than 0."""
if number < 0: raise NegativeNumberError, "Square root of negative number not permitted"
return math.sqrt( number )
while 1: try: userValue = float( raw_input( "nPlease enter a number: " ) ) print squareRoot( userValue ) except ValueError: print "The entered value is not a number" except NegativeNumberError, exception: print exception else: break
Related Scripts with Example Source Code in same category :