10–1. Throws an exception. Which of the following causes an exception to be thrown when the program executes? Notice what we're asking here is not a different
The usual reason.
A) Users
b) Interpreter
c) procedure
D) All of the above
e) (b) and C only)
f) only a) and C)
Answer: F
10–2. Throws an exception. Referring to the list of top issues, what factors throw an exception when the interaction interpreter is executed?
Answer: F
10–3. Key word. What are the keywords used to throw exceptions?
Answer: Raise
10–4. Key word. What's the difference between try-except and try-finally?
For:
You can use the Try-except statement to detect and handle exceptions. You can also add an optional ELSE clause to handle the
Code that executes when an exception is detected. Try-finally only allows detection of anomalies and does some necessary cleanup work (regardless
Error or not), no exception handling facilities
10–5. Abnormal. What exceptions will be thrown by the Python code snippet below these interaction interpreters (see table 10.2
List of built-in exceptions given):
(a) >>> if 3 < 4 Then:print ' 3 is less than 4! '
(b) >>> alist = [' Hello ', ' world! ', ' anyone ', ' Home ']
>>> print ' The last string in Alist is: ', Alist[len (alist)]
(c) >>> X
(d) >>> x = 4 0
(e) >>> import Math
>>> i = math.sqrt (-1)
For:
(a) Syntaxerror:invalid syntax
(b) indexerror:list index out of range
(c) nameerror:name ' x ' is not defined
(d) Zerodivisionerror:integer division or modulo by zero
(e) valueerror:math domain error
10–6. Improved open (). Create a wrapper for the built-in open () function. After the file is successfully opened, the return
Back to the file handle; If open fails, it is returned to the caller None, instead of generating an exception. This way, you don't need to open the file.
An extra exception-handling statement is needed.
For:
defSafe_open (name = None, mode='R'): Try: F=Open (Name,mode)exceptioerror:f=NoneElse: returnFif __name__=="__main__": F= Safe_open ('1.txt') PrintF
Operation Result:
Slightly
10–7. Abnormal. What is the difference between the following two-segment Python pseudo-code a) and B? Consider the top and bottom of statements A and B
Environment. (Such a meticulous distinction to thank Guido)
(a) Try:
Statement_a
Except ...:
. . .
Else
Statement_b
(b) Try:
Statement_a
Statement_b
Except ...:
. . .
10–8. Improved Raw_input (). At the beginning of this chapter, we give a "safe" float () function,
It is built on the built-in function float () to detect and process two different exceptions that float () may throw. Same
The Raw_input () function may also generate two exceptions, Eoferror (EOF at the end of the file, under Unix because it was pressed
Ctrl+d in Dos is because CTRL + Z) or keyboardinterrupt (cancel the input, generally because the press
CTRL + C). Please create a wrapper function, Safe_input (), to return None when an exception occurs.
For:
def Safe_input (prompt): try : F = Raw_input (prompt)
except
eoferror,keyboardinterrupt:f =
None
return
f
if
__name__ = =
"
__main__
: F = Safe_input ( '
10–9. Improved Math.sqrt (). The Math module contains a number of functions and constants for handling numeric-related operations. No
Fortunately, it does not recognize the plural, so we created the Cmath module to support complex correlation operations. Please create a
The Safe_sqrt () function, which encapsulates math.sqrt () and can handle negative values, returns a corresponding complex number.
For:
ImportMathImportCmathdefsafe_sqrt (num):Try: Ret=math.sqrt (num)exceptValueerror:ret=cmath.sqrt (num)returnretif __name__=="__main__": PrintSAFE_SQRT (4) PrintSAFE_SQRT (-4)