10.14. Exercises

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

a) the user

b) the interpreter

c) the program

d) all of the above

e) only (b) and (c)

f) only (a) and (c)

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

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

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

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)

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.

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 …:
       …

Improving raw_input(). In 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 which 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.

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. Rather than suffering the overhead of importing an entire module for complex numbers which you do not plan on using in your application, you want to just use the standard arithmetic operators which work fine with complex numbers, but really want just a square root function that can provide a complex number result when given a negative argument. 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset