Write a Python program that shows the importance of the order of exception handlers. Write two programs, one with the correct order of except handlers and another with an order that causes a logic error. If you attempt to catch a base-class exception type before a derived-class type, the pro- gram may produce a logic error.
What will be an ideal response?
```
# Correct order for exception handlers.
import time
class MenuError( ValueError ):
"""User selected an invalid menu choice"""
pass
# print menu instructions
def printMenu():
print """Menu:
[1] Print seconds since epoch
[2] Print formatted time
[3] Quit"""
# get a menu selection from user
# raise MenuError on incorrect choice
# raise ValueError if user does not enter integer
# raise SystemExit if user wants to quit
def getSelection():
selection = int( raw_input( "Choice: " ) )
# handle user input
if selection == 1:
print time.time()
# format time
elif selection == 2:
print time.ctime( time.time() )
# quit
elif selection == 3:
raise SystemExit
# incorrect input
else:
raise MenuError, "Value %d not in range [1, 3]" % selection
# main program
printMenu()
# keep trying until user selects a proper menu item
while 1:
try:
getSelection()
break
# incorrect menu choice
except MenuError:
print "Invalid menu choice!\n"
printMenu()
# not integer choice
except ValueError, exception:
print "***%s***" % exception
# user wants to quit
except SystemExit:
print "Goodbye!"
break
```
Menu:
[1] Print seconds since epoch
[2] Print formatted time
[3] Quit
Choice: -1
Invalid menu choice!
Menu:
[1] Print seconds since epoch
[2] Print formatted time
[3] Quit
Choice: a
***invalid literal for int(): a***
Choice: 1
1010071347.53
Menu:
[1] Print seconds since epoch
[2] Print formatted time
[3] Quit
Choice: 3
Goodbye!
```
# Incorrect order for exception handlers--menu not printed
# on incorrect choice.
import time
class MenuError( ValueError ):
"""User selected an invalid menu choice"""
pass
# print menu instructions
def printMenu():
print """Menu:
[1] Print seconds since epoch
[2] Print formatted time
[3] Quit"""
# get a menu selection from user
# raise MenuError on incorrect choice
# raise ValueError if user does not enter integer
# raise SystemExit if user wants to quit
def getSelection():
selection = int( raw_input( "Choice: " ) )
# handle user input
if selection == 1:
print time.time()
# format time
elif selection == 2:
print time.ctime( time.time() )
# quit
elif selection == 3:
raise SystemExit
# incorrect input
else:
raise MenuError, "Value %d not in range [1, 3]" % selection
# main program
printMenu()
# keep trying until user selects a proper menu item
while 1:
# get user input
try:
getSelection()
break
# incorrectly placed before MenuError
# not integer choice
except ValueError, exception:
print "***%s***" % exception
# incorrect menu choice
except MenuError:
print "Invalid menu choice!\n"
printMenu()
# user wants to quit
except SystemExit:
print "Goodbye!"
break
```
Menu:
[1] Print seconds since epoch
[2] Print formatted time
[3] Quit
Choice: a
***invalid literal for int(): a***
Choice: 5
***Value 5 not in range [1, 3]***
Choice: 3
Goodbye!
You might also like to view...
Stream reduction operation ________ uses the elements of a collection to produce a single value using an associative accumulation function (e.g., a lambda that adds two elements).
a. reduce b. condense c. combine d. associate
Your original work receives copyright protection as soon as you receive notification from the Copyright Office.
Answer the following statement true (T) or false (F)
A pipelined computer has a four?stage pipeline: fetch/decode, operand fetch, execute, writeback. All operations except load and branch do not introduce stalls. A load introduces one stall cycle. A non?taken branch introduces not stalls and a taken branch introduces two stall cycles. Consider the following loop.
for (j=1023; j > 0; j--) {x[j]=x[j]+2;} a. Express this code in an ARM?like assembly language (assume that you cannot use autoindexed addressing and that the only addressing mode is register indirect of the form [r0]). b. Show a single trip round the loop and indicate how many clock cycles are required. c. How many cycles will it take to execute this code in total? d. How can you modify the code to reduce the number of cycles?
What does httpclient.execute returns in android?
A - Http entity B - Http response C - Http result D - None of the above.