Section 10.14. Exercises


10.14. Exercises

10-1.

Raising Exceptions. Which of the following can raise exceptions during program execution? Note that this question does not ask what may cause exceptions.

  1. The user (of your program)

  2. The interpreter

  3. The program(er)

  4. All of the above

  5. Only (b) and (c)

  6. Only (a) and (c)

10-2.

Raising Exceptions. Referring to the list in the problem above, which could raise exceptions while running within the interactive interpreter?

10-3.

Keywords. Name the keyword(s) which is (are) used to raise exceptions.

10-4.

Keywords. What is the difference between try-except and TRy-finally?

10-5.

Exceptions. Name the exception that would result from executing the following pieces of Python code from within the interactive interpreter (refer back to Table 10.2 for a list of all built-in exceptions):

(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)


10-6.

Improving open(). Create a wrapper for the open() function. When a program opens a file successfully, a file handle will be returned. If the file open fails, rather than generating an error, return None to the callers so that they can open files without an exception handler.

10-7.

Exceptions. What is the difference between Python pseudocode snippets (a) and (b)? Answer in the context of statements A and B, which are part of both pieces of code. (Thanks to Guido for this teaser!)

(a)  try:         statement_A    except . . .:        . . .      else:         statement_B (b)  try:         statement_A         statement_B     except . . .:            . . .


10-8.

Improving raw_input(). At the beginning of this chapter, we presented a "safe" version of the float() built-in function to detect and handle two different types of exceptions that float() generates. Likewise, the raw_input() function can generate two different exceptions, either EOFError or KeyboardInterrupt on end-of-file (EOF) or cancelled input, respectively. Create a wrapper function, perhaps safe_ input(); rather than raising an exception if the user entered EOF (^D in Unix or ^Z in DOS) or attempted to break out using ^C, have your function return None that the calling function can check for.

10-9.

Improving math.sqrt(). The math module contains many functions and some constants for performing various mathematics-related operations. Unfortunately, this module does not recognize or operate on complex numbers, which is the reason why the cmath module was developed. Create a function, perhaps safe_sqrt(), which wraps math.sqrt(), but is smart enough to handle a negative parameter and return a complex number with the correct value back to the caller.



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net