打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Chapter 7:  Exception Handling and Debugging

Any good program makes use of a language’s exception handling mechanisms. Thereis no better way to frustrate an end-user then by having them run into an issuewith your software and displaying a big ugly error message on the screen,followed by a program crash. Exception handling is all about ensuring that whenyour program encounters an issue, it will continue to run and provideinformative feedback to the end-user or program administrator. Any Javaprogrammer becomes familiar with exception handling on day one, as some Javacode won’t even compile unless there is some form of exception handling putinto place via the try-catch-finally syntax. Python has similar constructs tothat of Java, and we’ll discuss them in this chapter.

After you have found an exception, or preferably before your software isdistributed, you should go through the code and debug it in order to find andrepair the erroneous code. There are many different ways to debug and repaircode; we will go through some debugging methodologies in this chapter. InPython as well as Java, the assert keyword can help out tremendously in thisarea. We’ll cover assert in depth here and learn the different ways that it canbe used to help you out and save time debugging those hard-to-find errors.

Exception Handling Syntax and Differences with Java

Java developers are very familiar with the try-catch-finally block as this isthe main mechanism that is used to perform exception handling. Python exceptionhandling differs a bit from Java, but the syntax is fairly similar. However,Java differs a bit in the way that an exception is thrown in code. Now, realizethat I just used the term throw…this is Java terminology. Python does not throwexceptions, but instead it raises them. Two different terms which meanbasically the same thing. In this section, we’ll step through the process ofhandling and raising exceptions in Python code, and show you how it differsfrom that in Java.

For those who are unfamiliar, I will show you how to perform some exceptionhandling in the Java language. This will give you an opportunity to compare thetwo syntaxes and appreciate the flexibility that Python offers.

Listing 7-1. Exception Handling in Java

try {// perform some tasks that may throw an exception} catch (ExceptionType messageVariable) {// perform some exception handling} finally {// execute code that must always be invoked}

Now let’s go on to learn how to make this work in Python. Not only will we seehow to handle and raise exceptions, but you’ll also learn some other greattechniques such as using assertions later in the chapter.

Catching Exceptions

How often have you been working in a program and performed some action thatcaused the program to abort and display a nasty error message? It happens moreoften than it should because most exceptions can be caught and handled nicely.By nicely, I mean that the program will not abort and the end user will receivea descriptive error message stating what the problem is, and in some cases howit can be resolved. The exception handling mechanisms within programminglanguages were developed for this purpose.

Listing 7-2. try-except Example

# This function uses a try-except clause to provide a nice error# message if the user passes a zero in as the divisor>>> from __future__ import division>>> def divide_numbers(x, y):...     try:...         return x/y...     except ZeroDivisionError:...         return 'You cannot divide by zero, try again'…# Attempt to divide 8 by 3>>> divide_numbers(8,3)2.6666666666666665# Attempt to divide 8 by zero>>> divide_numbers(8, 0)'You cannot divide by zero, try again'

Table 7-1 lists of all exceptions that are built into the Python language alongwith a description of each. You can write any of these into an except clauseand try to handle them. Later in this chapter I will show you how you and raisethem if you’d like. Lastly, if there is a specific type of exception that you’dlike to throw that does not fit any of these, then you can write your ownexception type object. It is important to note that Python exception handlingdiffers a bit from Java exception handling. In Java, many times the compilerforces you to catch exceptions, such is known as checked exceptions. Checkedexceptions are basically exceptions that a method may throw while performingsome task. The developer is forced to handle these checked exceptions using atry/catch or a throws clause, otherwise the compiler complains. Python has nosuch facility built into its error handling system. The developer decides whento handle exceptions and when not to do so. It is a best practice to includeerror handling wherever possible even though the interpreter does not force it.

Exceptions in Python are special classes that are built into the language. Assuch, there is a class hierarchy for exceptions and some exceptions areactually subclasses of another exception class. In this case, a program canhandle the superclass of such an exception and all subclassed exceptions arehandled automatically. Table 7-1 lists the exceptions defined in the Pythonlanguage, and the indentation resembles the class hierarchy.

Table 7-1. Exceptions

ExceptionDescription
BaseExceptionThis is the root exception for all others
GeneratorExitRaised by close() method of generators for terminatingiteration
KeyboardInterruptRaised by the interrupt key
SystemExitProgram exit
ExceptionRoot for all non-exiting exceptions
StopIterationRaised to stop an iteration action
StandardErrorBase class for all built-in exceptions
ArithmeticErrorBase for all arithmetic exceptions
FloatingPointErrorRaised when a floating-point operation fails
OverflowErrorArithmetic operations that are too large
ZeroDivisionErrorDivision or modulo operation with zero as divisor
AssertionErrorRaised when an assert statement fails
AttributeErrorAttribute reference or assignment failure
EnvironmentErrorAn error occurred outside of Python
IOErrorError in Input/Output operation
OSErrorAn error occurred in the os module
EOFErrorinput() or raw_input() tried to read past the end of afile
ImportErrorImport failed to find module or name
LookupErrorBase class for IndexError and KeyError
IndexErrorA sequence index goes out of range
KeyErrorReferenced a non-existent mapping (dict) key
MemoryErrorMemory exhausted
NameErrorFailure to find a local or global name
UnboundLocalErrorUnassigned local variable is referenced
ReferenceErrorAttempt to access a garbage-collected object
RuntimeErrorObsolete catch-all error
NotImplementedErrorRaised when a feature is not implemented
SyntaxErrorParser encountered a syntax error
IndentationErrorParser encountered an indentation issue
TabErrorIncorrect mixture of tabs and spaces
SystemErrorNon-fatal interpreter error
TypeErrorInappropriate type was passed to an operator or function
ValueErrorArgument error not covered by TypeError or a more preciseerror
WarningBase for all warnings

The try-except-finally block is used in Python programs to perform theexception-handling task. Much like that of Java, code that may or may not raisean exception can be placed in the try block. Differently though, exceptionsthat may be caught go into an except block much like the Java catch equivalent.Any tasks that must be performed no matter if an exception is thrown or notshould go into the finally block. All tasks within the finally block areperformed if an exception is raised either within the except block or by someother exception. The tasks are also performed before the exception is raised toensure that they are completed. The finally block is a great place to performcleanup activity such as closing open files and such.

Listing 7-3. try-except-finally Logic

try:    # perform some task that may raise an exceptionexcept Exception, value:    # perform some exception handlingfinally:    # perform tasks that must always be completed (Will be performed before the exception is # raised.)

Python also offers an optional else clause to create the try-except-else logic.This optional code placed inside the else block is run if there are noexceptions found in the block.

Listing 7-4. try-finally logic

try:    # perform some tasks that may raise an exceptionfinally:    # perform tasks that must always be completed (Will be performed before the exception is # raised.)

The else clause can be used with the exception handling logic to ensure thatsome tasks are only run if no exceptions are raised. Code within the elseclause is only initiated if no exceptions are thrown, and if any exceptions areraised within the else clause the control does not go back out to the except.Such activities to place in inside an else clause would be transactions such asa database commit. If several database transactions were taking place insidethe try clause you may not want a commit to occur unless there were noexceptions raised.

Listing 7-5. try-except-else logic:

try:    # perform some tasks that may raise an exceptionexcept:    # perform some exception handlingelse:    # perform some tasks thatwill only be performed if no exceptions are thrown

You can name the specific type of exception to catch within the except block,or you can generically define an exception handling block by not naming anyexception at all. Best practice of course states that you should always try toname the exception and then provide the best possible handling solution for thecase. After all, if the program is simply going to spit out a nasty error thenthe exception handling block is not very user friendly and is only helpful todevelopers. However, there are some rare cases where it would be advantageousto not explicitly refer to an exception type when we simply wish to ignoreerrors and move on. The except block also allows us to define a variable towhich the exception message will be assigned. This allows us the ability tostore that message and display it somewhere within our exception handling codeblock. If you are calling a piece of Java code from within Jython and the Javacode throws an exception, it can be handled within Jython in the same manner asJython exceptions.

Listing 7-6. Exception Handling in Python

# Code without an exception handler>>> x = 10>>> z = x / yTraceback (most recent call last):  File "<stdin>", line 1, in <module>NameError: name 'y' is not defined# The same code with an exception handling block>>> x = 10>>> try:...     z = x / y... except NameError, err:...     print "One of the variables was undefined: ", err...One of the variables was undefined:  name 'y' is not defined

It is important to note that Jython 2.5.x uses the Python 2.5.x exceptionhandling syntax. This syntax will be changing in future releases of Jython.Take note of the syntax that is being used for defining the variable that holdsthe exception. Namely, the except ExceptionType, value statement syntax inPython and Jython 2.5 differs from that beyond 2.5. In Python 2.6, the syntaxchanges a bit in order to ready developers for Python 3, which exclusively usesthe new syntax.

Listing 7-7. Jython and Python 2.5 and Prior

try:    # codeexcept ExceptionType, messageVar:    # code

Listing 7-8. Jython 2.6 (Not Yet Implemented) and Python 2.6 and Beyond

try:   # codeexcept ExceptionType as messageVar:    # code

We had previously mentioned that it was simply bad programming practice to notexplicitly name an exception type when writing exception handling code. This istrue, however Python provides us with another a couple of means to obtain thetype of exception that was thrown. The easiest way to find an exception type isto simply catch the exception as a variable as we’ve discussed previously. Youcan then find the specific exception type by using the type(error_variable)syntax if needed.

Listing 7-9. Determining Exception Type

# In this example, we catch a general exception and then determine the typelater>>> try:...     8/0... except Exception, ex1:...     'An error has occurred'...'An error has occurred'>>> ex1ZeroDivisionError('integer division or modulo by zero',)>>> type(ex1)<type 'exceptions.ZeroDivisionError'>>>>

There is also a function provided in the sys package known as sys.exc_info()that will provide us with both the exception type and the exception message.This can be quite useful if we are wrapping some code in a try-except block butwe really aren’t sure what type of exception may be thrown. Below is an exampleof using this technique.

Listing 7-10. Using sys.exc_info()

# Perform exception handling without explicitly naming the exception type>>> x = 10>>> try:...     z = x / y... except:...     print "Unexpected error: ", sys.exc_info()[0], sys.exc_info()[1]...Unexpected error:  <type 'exceptions.NameError'> name 'y' is not defined

Sometimes you may run into a situation where it is applicable to catch morethan one exception. Python offers a couple of different options if you need todo such exception handling. You can either use multiple except clauses, whichdoes the trick and works well if you’re interested in performing differenttasks for each different exception that occurs, but may become too wordy. Theother preferred option is to enclose your exception types within parenthesesand separated by commas on your except statement. Take a look at the followingexample that portrays the latter approach using Listing 7-6.

Listing 7-11. Handling Multiple Exceptions

# Catch NameError, but also a ZeroDivisionError in case a zero is used in theequation>>> try:...     z = x/y... except(NameError, ZeroDivisionError), err:...     "An error has occurred, please check your values and try again"...'An error has occurred, please check your values and try again'# Using multiple except clauses>>> x = 10>>> y = 0>>> try:...     z = x / y... except NameError, err1:...     print err1... except ZeroDivisionError, err2:...     print 'You cannot divide a number by zero!'...You cannot divide a number by zero!

As mentioned previously, an exception is simply a class in Python. There aresuperclasses and subclasses for exceptions. You can catch a superclassexception to catch any of the exceptions that subclass that exception arethrown. For instance, if a program had a specific function that accepted eithera list or dict object, it would make sense to catch a LookupError as opposed tofinding a KeyError or IndexError separately. Look at the following example tosee one way that this can be done.

Listing 7-12. Catching a Superclass Exceptions

# In the following example, we define a function that will return# a value from some container.  The function accepts either lists# or dictionary objects.  The LookupError superclass is caught# as opposed to checking for each of it's subclasses...namely KeyError and IndexError.>>> def find_value(obj, value):...     try:...         return obj[value]...     except LookupError, ex:...         return 'An exception has been raised, check your values and try again'...# Create both a dict and a list and test the function by looking for a value that does# not exist in either container>>> mydict = {'test1':1,'test2':2}>>> mylist = [1,2,3]>>> find_value(mydict, 'test3')'An exception has been raised, check your values and try again'>>> find_value(mylist, 2)3>>> find_value(mylist, 3)'An exception has been raised, check your values and try again'>>>

If multiple exception blocks have been coded, the first matching exception isthe one that is caught. For instance, if we were to redesign the find_valuefunction that was defined in the previous example, but instead raised eachexception separately then the first matching exception would be raised. . .theothers would be ignored. Let’s see how this would work.

Listing 7-13. Catching the First Matching Exceptions

# Redefine the find_value() function to check for each exception separately# Only the first matching exception will be raised, others will be ignored.# So in these examples, the except LookupError code is never run.>>> def find_value(obj, value):...     try:...        return obj[value]...     except KeyError:...         return 'The specified key was not in the dict, please try again'...     except IndexError:...         return 'The specified index was out of range, please try again'...     except LookupError:...         return 'The specified key was not found, please try again'...>>> find_value(mydict, 'test3')'The specified key was not in the dict, please try again'>>> find_value(mylist, 3)'The specified index was out of range, please try again'>>>

The try-except­ block can be nested as deep as you’d like. In the case ofnested exception handling blocks, if an exception is thrown then the programcontrol will jump out of the inner most block that received the error, and upto the block just above it. This is very much the same type of action that istaken when you are working in a nested loop and then run into a breakstatement, your code will stop executing and jump back up to the outer loop.The following example shows an example for such logic.

Listing 7-14. Nested Exception Handling Blocks

# Perform some division on numbers entered by keyboardtry:    # do some work    try:        x = raw_input ('Enter a number for the dividend:  ')        y = raw_input('Enter a number to divisor: ')        x = int(x)        y = int(y)    except ValueError:        # handle exception and move to outer try-except        print 'You must enter a numeric value!'    z = x / yexcept ZeroDivisionError:    # handle exception    print 'You cannot divide by zero!'except TypeError:    print 'Retry and only use numeric values this time!'else:    print 'Your quotient is: %d' % (z)

In the previous example, we nested the different exception blocks. If the firstValueError were raised, it would give control back to the outer exceptionblock. Therefore, the ZeroDivisionError and TypeError could still be raised.Otherwise, if those last two exceptions are not thrown then the tasks withinthe else clause would be run.

As stated previously, it is a common practice in Jython to handle Javaexceptions. Oftentimes we have a Java class that throws exceptions, and thesecan be handled or displayed in Jython just the same way as handling Pythonexceptions.

Listing 7-15. Handling Java Exceptions in Jython

// Java Class TaxCalcpublic class TaxCalc {    public static void main(String[] args) {    double cost = 0.0;    int pct   = 0;    double tip = 0.0;    try {        cost = Double.parseDouble(args[0]);        pct = Integer.parseInt(args[1]);        tip = (cost * (pct * .01));        System.out.println("The total gratutity based on " + pct + " percent would be " + tip);        System.out.println("The total bill would be " + (cost + tip) );    } catch (NumberFormatException ex){        System.out.println("You must pass number values as arguments.  Exception: " + ex);    } catch (ArrayIndexOutOfBoundsException ex1){        System.out.println("You must pass two values to this utility.  " +        "Format: TaxCalc(cost, percentage)  Exception: " + ex1);    }    }}

Using Jython:

# Now lets bring the TaxCalc Java class into Jython and use it>>> import TaxCalc>>> calc = TaxCalc()# pass strings within a list to the TaxCalc utility and the Java exception will be thrown>>> vals = ['test1','test2']>>> calc.main(vals)You must pass number values as arguments.  Exception:java.lang.NumberFormatException: For input string: "test1"# Now pass numeric values as strings in a list, this works as expected (except for the bad# rounding)>>> vals = ['25.25', '20']>>> calc.main(vals)The total gratutity based on 20 percent would be 5.050000000000001The total bill would be 30.3

You can also throw Java exceptions in Jython by simply importing them first andthen using then raising them just like Python exceptions.

Raising Exceptions

Often you will find reason to raise your own exceptions. Maybe you areexpecting a certain type of keyboard entry, and a user enters somethingincorrectly that your program does not like. This would be a case when you’dlike to raise your own exception. The raise statement can be used to allow youto raise an exception where you deem appropriate. Using the raise statement,you can cause any of the Python exception types to be raised, you could raiseyour own exception that you define (discussed in the next section). The raisestatement is analogous to the throw statement in the Java language. In Java wemay opt to throw an exception if necessary. However, Java also allows you toapply a throws clause to a particular method if an exception may possibly bethrown within instead of using try-catch handler in the method. Python does notallow you do perform such techniques using the raise statement.

Listing 7-16. raise Statement Syntax

raise ExceptionType or String[, message[, traceback]]

As you can see from the syntax, using raise allows you to become creative inthat you could use your own string when raising an error. However, this is notreally looked upon as a best practice as you should try to raise a definedexception type if at all possible. You can also provide a short messageexplaining the error. This message can be any string. Let’s take a look at anexample.

Listing 7-17. raising Exceptions Using Message

>>> raise Exception("An exception is being raised")Traceback (most recent call last):  File "<stdin>", line 1, in <module>Exception: An exception is being raised>>> raise TypeError("You've specified an incorrect type")Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: You've specified an incorrect type

Now you’ve surely seen some exceptions raised in the Python interpreter by now.Each time an exception is raised, a message appears that was created by theinterpreter to give you feedback about the exception and where the offendingline of code may be. There is always a traceback section when any exception israised. This really gives you more information on where the exception wasraised. Lastly, let’s take a look at raising an exception using a differentformat. Namely, we can use the format raise Exception, “message”.

Listing 7-18. Using the raise Statement with the Exception, “message” Syntax

>>> raise TypeError,"This is a special message"Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: This is a special message

Defining Your Own Exceptions

You can define your own exceptions in Python by creating an exception class.You simply define a class that inherits from the base Exception class. Theeasiest defined exception can simply use a pass statement inside the class.Exception classes can accept parameters using the initializer, and return theexception using the __str__ method. Any exception you write should accept amessage. It is also a good practice to name your exception giving it a suffixof Error if the exception is referring to an error of some kind.

Listing 7-19. Defining a Basic Exception Class

class MyNewError(Exception):    pass

This example is the simplest type of exception you can create. This exceptionthat was created above can be raised just like any other exception now.

raise MyNewError("Something happened in my program")

A more involved exception class may be written as follows.

Listing 7-20. Exception Class Using Initializer

class MegaError(Exception):    """ This is raised when there is a huge problem with my program"""    def __init__(self, val):        self.val = val    def __str__(self):        return repr(self.val)

Issuing Warnings

Warnings can be raised at any time in your program and can be used to displaysome type of warning message, but they do not necessarily cause execution toabort. A good example is when you wish to deprecate a method or implementationbut still make it usable for compatibility. You could create a warning to alertthe user and let them know that such methods are deprecated and point them tothe new definition, but the program would not abort. Warnings are easy todefine, but they can be complex if you wish to define rules on them usingfilters. Warning filters are used to modify the behavior of a particularwarning. Much like exceptions, there are a number of defined warnings that canbe used for categorizing. In order to allow these warnings to be easilyconverted into exceptions, they are all instances of the Exception type.Remember that exceptions are not necessarily errors, but rather alerts ormessages. For instance, the StopIteration exception is raised by a program tostop the iteration of a loop…not to flag an error with the program.

To issue a warning, you must first import the warnings module into yourprogram. Once this has been done then it is as simple as making a call to thewarnings.warn() function and passing it a string with the warning message.However, if you’d like to control the type of warning that is issued, you canalso pass the warning class. Warnings are listed in Table 7-2.

Listing 7-21. Issuing a Warning

# Always import the warnings module firstimport warnings# A couple of examples for setting up warningswarnings.warn("this feature will be deprecated")warnings.warn("this is a more involved warning", RuntimeWarning)# Using A Warning in a Function# Suppose that use of the following function has been deprecated,# warnings can be used to alert the function users# The following function calculates what the year will be if we# add the specified number of days to the current year.  Of course,# this is pre-Y2K code so it is being deprecated.  We certainly do not# want this code around when we get to year 3000!>>> def add_days(current_year, days):...     warnings.warn("This function has been deprecated as of version x.x",DeprecationWarning)...     num_years = 0...     if days > 365:...         num_years = days/365...     return current_year + num_years...# Calling the function will return the warning that has been set up,# but it does not raise an error...the expected result is still returned.>>> add_days(2009, 450)__main__:2: DeprecationWarning: This function has been deprecated as of version x.x2010

Table 7-2. Python Warning Categories

WarningDescription
WarningRoot warning class
UserWarningA user-defined warning
DeprecationWarningWarns about use of a deprecated feature
SyntaxWarningSyntax issues
RuntimeWarningRuntime issues
FutureWarningWarns that a particular feature will be changing in afuture release

Importing the warnings module into your code gives you access to a number ofbuilt-in warning functions that can be used. If you’d like to filter a warningand change its behavior then you can do so by creating a filter. Table 7-3lists functions that come with the warnings module.

Table 7-3. Warning Functions

FunctionDescription
warn(message[,category[,stacklevel]])Issues a warning. Parameters include a messagestring, the optional category of warning, and theoptional stack level that tells which stack frame thewarning should originate from, usually either thecalling function or the source of the functionitself.
warn_explicit(message,category, filename,lineno[, module[,registry]])This offers a more detailed warning message and makescategory a mandatory parameter. filename, lineno, andmodule tell where the warning is located. registryrepresents all of the current warning filters thatare active.
showwarning(message,category, filename,lineno[, file])Gives you the ability to write the warning to a file.
formatwarning(message,category, filename,lineno)Creates a formatted string representing the warning.
simplefilter(action[,category[, lineno[,append]]])Inserts simple entry into the ordered list ofwarnings filters. Regular expressions are not neededfor simplefilter as the filter always matches anymessage in any module as long as the category andline number match. filterwarnings() described belowuses a regular expression to match against warnings.
resetwarnings()Resets all of the warning filters.
filterwarnings(action[,message[, category[,module[, lineno[,append]]]]])This adds an entry into a warning filter list.Warning filters allow you to modify the behavior of awarning. The action in the warning filter can be onefrom those listed in Table 7-4, message is a regularexpression, category is the type of a warning to beissued, module can be a regular expression, lineno isa line number to match against all lines, appendspecifies whether the filter should be appended tothe list of all filters.

Table 7-4. Python Filter Actions

Filter Actions 
‘always’Always print warning message
‘default’Print warning once for each location where warning occurs
‘error’Converts a warning into an exception
‘ignore’Ignores the warning
‘module’Print warning once for each module in which warning occurs
‘once’Print warning only one time

Let’s take a look at a few ways to use warning filters in the examples below.

Listing 7-22. Warning Filter Examples

# Set up a simple warnings filter to raise a warning as an exception>>> warnings.simplefilter('error', UserWarning)>>> warnings.warn('This will be raised as an exception')Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "/Applications/Jython/jython2.5.1rc2/Lib/warnings.py", line 63, in warn    warn_explicit(message, category, filename, lineno, module, registry,  File "/Applications/Jython/jython2.5.1rc2/Lib/warnings.py", line 104, in warn_explicit    raise messageUserWarning: This will be raised as an exception# Turn off all active filters using resetwarnings()>>> warnings.resetwarnings()>>> warnings.warn('This will not be raised as an exception')__main__:1: UserWarning: This will not be raised as an exception# Use a regular expression to filter warnings# In this case, we ignore all warnings containing the word “one”>>> warnings.filterwarnings('ignore', '.*one*.',)>>> warnings.warn('This is warning number zero')__main__:1: UserWarning: This is warning number zero>>> warnings.warn('This is warning number one')>>> warnings.warn('This is warning number two')__main__:1: UserWarning: This is warning number two>>>

There can be many different warning filters in use, and each call to thefilterwarnings() function will append another warning to the ordered list offilters if so desired. The specific warning is matched against each filterspecification in the list in turn until a match is found. In order to see whichfilters are currently in use, issue the command print warnings.filters. One canalso specify a warning filter from the command line by use of the –W option.Lastly, all warnings can be reset to defaults by using the resetwarnings()function.

It is also possible to set up a warnings filter using a command-line argument.This can be quite useful for filtering warnings on a per-script or per-modulebasis. For instance, if you are interested in filtering warnings on aper-script basis then you could issue the -W command line argument whileinvoking the script.

Listing 7-23. -W command-line option

-Waction:message:category:module:lineno

Listing 7-24. Example of using W command line option

# Assume we have the following script test_warnings.py# and we are interested in running it from the command lineimport warningsdef test_warnings():    print "The function has started"    warnings.warn("This function has been deprecated", DeprecationWarning)    print "The function has been completed"if __name__ == "__main__":    test_warnings()# Use the following syntax to start and run jython as usual without# filtering any warningsjython test_warnings.pyThe function has startedtest_warnings.py:4: DeprecationWarning: This function has been deprecated  warnings.warn("This function has been deprecated", DeprecationWarning)The function has been completed# Run the script and ignore all deprecation warningsjython -W "ignore::DeprecationWarning::0" test_warnings.pyThe function has startedThe function has been completed# Run the script one last time and treat the DeprecationWarning# as an exception.  As you see, it never completesjython -W "error::DeprecationWarning::0" test_warnings.pyThe function has startedTraceback (most recent call last):  File "test_warnings.py", line 8, in <module>    test_warnings()  File "test_warnings.py", line 4, in test_warnings    warnings.warn("This function has been deprecated", DeprecationWarning)  File "/Applications/Jython/jython2.5.1rc2/Lib/warnings.py", line 63, in warn    warn_explicit(message, category, filename, lineno, module, registry,  File "/Applications/Jython/jython2.5.1rc2/Lib/warnings.py", line 104, in warn_explicit    raise messageDeprecationWarning: This function has been deprecated

Warnings can be very useful in some situations. They can be made as simplisticor sophisticated as need be.

Assertions and Debugging

Debugging can be an easy task in Python via use of the assert statement. InCPython, the __debug__ variable can also be used, but this feature is currentlynot usable in Jython as there is no optimization mode for the interpreter. .Assertions are statements that can print to indicate that a particular piece ofcode is not behaving as expected. The assertion checks an expression for a Trueor False value, and if it evaluates to False in a Boolean context then itissues an AssertionError along with an optional message. If the expressionevaluates to True then the assertion is ignored completely.

assert expression [, message]

By effectively using the assert statement throughout your program, you caneasily catch any errors that may occur and make debugging life much easier.Listing 7-25 will show you the use of the assert statement.

Listing 7-25. Using assert

#  The following example shows how assertions are evaluated>>> x = 5>>> y = 10>>> assert x < y, "The assertion is ignored">>> assert x > y, "The assertion raises an exception"Traceback (most recent call last):  File "<stdin>", line 1, in <module>AssertionError: The assertion raises an exception# Use assertions to validate parameters# Here we check the type of each parameter to ensure# that they are integers>>> def add_numbers(x, y):...     assert type(x) is int, "The arguments must be integers, please check the first argument"...     assert type(y) is int, "The arguments must be integers, please check the second argument"...     return x + y...# When using the function, AssertionErrors are raised as necessary>>> add_numbers(3, 4)7>>> add_numbers('hello','goodbye')Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "<stdin>", line 2, in add_numbersAssertionError: The arguments must be integers, please check the first argument

Context Managers

Ensuring that code is written properly in order to manage resources such asfiles or database connections is an important topic. If files or databaseconnections are opened and never closed then our program could incur issues.Often times, developers elect to make use of the try-finally blocks to ensurethat such resources are handled properly. While this is an acceptable methodfor resource management, it can sometimes be misused and lead to problems whenexceptions are raised in programs. For instance, if we are working with adatabase connection and an exception occurs after we’ve opened the connection,the program control may break out of the current block and skip all furtherprocessing. The connection may never be closed in such a case. That is wherethe concept of context management becomes an important new feature in Jython.Context management via the use of the with statement is new to Jython 2.5, andit is a very nice way to ensure that resources are managed as expected.

In order to use the with statement, you must import from __future__. The withstatement basically allows you to take an object and use it without worryingabout resource management. For instance, let’s say that we’d like to open afile on the system and read some lines from it. To perform a file operation youfirst need to open the file, perform any processing or reading of file content,and then close the file to free the resource. Context management using the withstatement allows you to simply open the file and work with it in a concisesyntax.

Listing 7-26. Python with Statement Example

#  Read from a text file named players.txt>>> from __future__ import with_statement>>> with open('players.txt','r') as file:...     x = file.read()...>>> print xSports Team Management---------------------------------Josh – forwardJim – defense

In this example, we did not worry about closing the file because the contexttook care of that for us. This works with object that extends the contextmanagement protocol. In other words, any object that implements two methodsnamed __enter__() and __exit__() adhere to the context management protocol.When the with statement begins, the __enter__() method is executed. Likewise,as the last action performed when the with statement is ending, the __exit__()method is executed. The __enter__() method takes no arguments, whereas the__exit__() method takes three optional arguments type, value, and traceback.The __exit__() method returns a True or False value to indicate whether anexception was thrown. The as variable clause on the with statement is optionalas it will allow you to make use of the object from within the code block. Ifyou are working with resources such as a lock then you may not need theoptional clause.

If you follow the context management protocol, it is possible to create yourown objects that can be used with this technique. The __enter__() method shouldcreate whatever object you are trying to work if needed.

Listing 7-27. Creating a Simple Object That Follows Context Management Protocol

# In this example, my_object facilitates the context management protocol# as it defines an __enter__ and __exit__ methodclass my_object:    def __enter__(self):        # Perform setup tasks        return object    def __exit__(self, type, value, traceback):        # Perform cleanup

If you are working with an immutable object then you’ll need to create a copyof that object to work with in the __enter__() method. The __exit__() method onthe other hand can simply return False unless there is some other type ofcleanup processing that needs to take place. If an exception is raisedsomewhere within the context manager, then __exit__() is called with threearguments representing type, value, and traceback. However, if there are noexceptions raised then __exit__() is passed three None arguments. If __exit__()returns True, then any exceptions are “swallowed” or ignored, and executioncontinues at the next statement after the with-statement.

Summary

In this chapter, we discussed many different topics regarding exceptions andexception handling within a Python application. First, you learned theexception handling syntax of the ­try-except-finally­ code block and how it isused. We then discussed why it may be important to raise your own exceptions attimes and how to do so. That topic led to the discussion of how to define anexception and we learned that in order to do so we must define a class thatextends the Exception type object.

After learning about exceptions, we went into the warnings framework anddiscussed how to use it. It may be important to use warnings in such caseswhere code may be deprecated and you want to warn users, but you do not wish toraise any exceptions. That topic was followed by assertions and how assertionstatement can be used to help us debug our programs. Lastly, we touched uponthe topic of context managers and using the with statement that is new inJython 2.5.

In the next chapter you will delve into the arena of building larger programs,learning about modules and packages.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Effective Java Exceptions && 高效的Java异常处理
90%老手的都不知道,Python异常还能写得如此优雅!
A Guide to Proper Error Handling in JavaScript
python最重要的内建异常类名总结!
DBAzine.com - Interested Transaction List (ITL) Waits Demystified
Advanced Memory Allocation
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服