Use inheritance to create an exception base class and various exception-derived classes. Write a program to demonstrate that the except clause specifying the base class catches derived- class exceptions.
What will be an ideal response?
```
# Exercise using exception handling.
import math
class ExerciseError( StandardError ):
"""Base exception class for this exercise"""
pass
class ImproperFunctionNameError( ExerciseError ):
"""User has input a function name that is not valid"""
pass
class ImproperInputError( ExerciseError ):
"""User has input improper value"""
pass
class ImproperFunctionCallError( ExerciseError ):
"""User's input has failed for a function call"""
pass
# raise ImproperFunctionNameError if function not available
def getUserFunction( possibleFunctions ):
# get function name from user
functionName = raw_input(
"Enter a function in %s: " % possibleFunctions.keys() )
# entered function not available
if functionName not in possibleFunctions.keys():
raise ImproperFunctionNameError, \
"%s is not an acceptable choice" % functionName
return possibleFunctions[ functionName ]
# raise ImproperInputError if the value is not numeric
def getUserInput():
# get numerical input from user
argument = raw_input( "Enter a numerical value: " )
# raise exception if entered value is not a numeral
try:
numericalValue = float( argument )
return numericalValue
except ValueError:
raise ImproperInputError, "%s not a numerical value" \
% argument
# raise ImproperFunctionCallError if call unsuccessful
def applyFunction( functionName, argument ):
# call function with specified argument
try:
result = functionName( argument )
return result
# repackage exception with ImproperFunctionCallError
except Exception, exception:
raise ImproperFunctionCallError, "%s (%s)" % \
( exception.__class__, exception )
# main program
print """This program takes a numeric input and a math function
name and calls the function name with the input. If an error occurs,
the program prints a message.\n"""
functionDict = { "cos" : math.cos,
"sin" : math.sin,
"sqrt" : math.sqrt,
"tan" : math.tan }
# continue until user enters correct function name
while 1:
# catch ImproperFunctionNameError
try:
userFunction = getUserFunction( functionDict )
break
except ImproperFunctionNameError, error:
print "Improper function:", error
# continue until user enters acceptable argument and function
# call is successful
while 1:
# handle incorrect input
try:
userInput = getUserInput()
break
except ImproperInputError, error:
print "Improper input:", error
# apply selected function on entered input
try:
result = applyFunction( userFunction, userInput )
print "The result is:", result
except ImproperFunctionCallError, error:
print "An error occurred:", error
```
This program takes a numeric input and a math function
name and calls the function name with the input. If an error occurs,
the program prints a message.
Enter a function in ['tan', 'cos', 'sin', 'sqrt']: -1
Improper function: -1 is not an acceptable choice
Enter a function in ['tan', 'cos', 'sin', 'sqrt']: a
Improper function: a is not an acceptable choice
Enter a function in ['tan', 'cos', 'sin', 'sqrt']: acos
Improper function: acos is not an acceptable choice
Enter a function in ['tan', 'cos', 'sin', 'sqrt']: sqrt
Enter a numerical value: -1
An error occurred: exceptions.ValueError (math domain error)
This program takes a numeric input and a math function
name and calls the function name with the input. If an error occurs,
the program prints a message.
Enter a function in ['tan', 'cos', 'sin', 'sqrt']: sqrt
Enter a numerical value: 1
The result is: 1.0
You might also like to view...
The keyboard shortcut for Find and Replace is Ctrl+r
Indicate whether the statement is true or false
Which of the following is a form of input validation that looks for allowable characters or patterns and only allows those?
A. blacklisting B clipping levels C. whitelisting D. traffic policing