Write a Python program that illustrates reraising an exception.
What will be an ideal response?
```
# Re-raising exceptions.
import math
import random
class ResultList( list ):
"""Class that keeps track of mathematical results"""
def recordResult( self, function, argument ):
"""Append result of function to list"""
# apply function and append result to list
try:
result = function( argument )
self.append( result )
except Exception, exception:
self.append( exception )
raise exception
# main program
# create ResultList instance
resultList = ResultList()
print "Computing 10 cases..."
print "---------------------"
# compute 10 results
for i in range( 10 ):
# print test number and record result
try:
print "Test %d:" % ( i + 1 ),
resultList.recordResult(
math.sqrt, random.randrange( -2, 5 ) )
print "SUCCESS"
# print status if an error occurs
except ValueError:
print "ERROR"
print "\nPrint results..."
print "-------------------"
# iterate over results list and print results
for result in resultList:
print result
```
Computing 10 cases...
---------------------
Test 1: SUCCESS
Test 2: SUCCESS
Test 3: ERROR
Test 4: SUCCESS
Test 5: SUCCESS
Test 6: ERROR
Test 7: SUCCESS
Test 8: SUCCESS
Test 9: SUCCESS
Test 10: ERROR
Print results...
-------------------
0.0
1.41421356237
math domain error
1.41421356237
1.73205080757
math domain error
0.0
0.0
2.0
math domain error
You might also like to view...
Which of the following correctly uses C++11’s range-based for statement to iterate through every element of the array variable arr?
a. for (auto x : arr) b. foreach (x in arr) c. for (auto x; x < arr.length; x++) d. for x in arr
Which of the following operators determines if two object variables reference the same object?
a. = b. IsEqual c. Is d. SameAs
Which type of exception occurs when creating a DataInputStream for a nonexistent file?
a. FileNotExist b. FileNotExistException c. FileNotFound d. FileNotFoundException
In a(n) _____________ relationship, an object of a subclass can also be treated as an object of its superclass.
Fill in the blank(s) with the appropriate word(s).