A programmer-defined exception class.

2019-06-16 02:07栏目:技术创新

import math

class NegativeNumberError( ArithmeticError ):
   pass

def squareRoot( number ):
   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 )
   # float raises ValueError if input is not numerical
   except ValueError:
      print 'The entered value is not a number'
   # squareRoot raises NegativeNumberError if number is negative
   except NegativeNumberError, exception:
      print exception
   else:
      break

版权声明:本文由新浦京娱乐站_新浦京娱乐场官网_新浦京娱乐发布于技术创新,转载请注明出处:A programmer-defined exception class.