dEBUggINg THE ExTERNAl modUlE
Debugging isn’t hard, but it also isn’t as straightforward as you might think when working with
IronPython. The debugger won’t take you directly to an error. You can’t test variables using the
debugger from within the static language. In short, you have to poke and prod the external script to
discover what ails it. Fortunately, you do have three tools at your disposal for discovering errors.
➤
Exceptions
➤
➤
➤
print
Statements
➤
➤
An
ErrorListener
object
548592c15.indd 321
2/25/10 9:45:57 AM
www.finebook.ir
322
❘
CHAPTER 15
Using ironPython from other .net LangUages
Let’s begin with the easiest of the three tools. The static language application won’t ignore outright
errors in the script code. For example, you might have the following error in the script:
# Introduce an error.
print 1/0
If your code has this error (and it really
shouldn’t), you’ll see an exception dialog
box like the one shown in Figure 15-6.
Unfortunately, when you click View Detail,
the content of the View Detail dialog box
is nearly useless. The exception informa-
tion won’t tell you where to find the error
in your script. In fact, it may very well lead
you on a wild goose chase that ends in
frustration.
The name of the exception will provide
clues as to where the error might exist, but
you can’t confirm your suspicions without help. The only tool, besides vigorous script testing, is to
include
print
statements such as these in your code.
# Display the values of First and Second.
print ‘Values in IronPython Script’
print ‘First = ‘, First
print ‘Second = ‘, Second
When you run the script, you see the output shown in Figure 15-7. Most developers view
print
statements as a bit old school, but they do work if you use them correctly. Make sure you provide
enough information to know where the script is failing to perform as expected. Even so, using
print
statements may feel a bit like wandering around in the dark, so you should place an emphasis on
testing the script before you use it and after each change you make.
FIgURE 15-7:
Using print statements may seem old school, but they work.
In some cases, you might make a small change to a script and it stops running completely — you might
not see a script exception, just an indicator that something’s wrong because the application raises an
FIgURE 15-6:
The static language application displays
exceptions for your script.
548592c15.indd 322
2/25/10 9:45:57 AM
www.finebook.ir
Do'stlaringiz bilan baham: |