Implementing .NET Exceptions
In general, you want to avoid using .NET exceptions in your IronPython applications, except in those
cases where you need to provide specific functionality for .NET code. The problem is that IronPython
views such exceptions from a Python perspective. Consequently, trapping .NET exceptions can prove
tricky unless you spend some time working with them in advance.
Many .NET exceptions are available in the
System
assembly so you need to import it before
you can perform any serious work. After that, you can
raise
a .NET exception much as you do
a Python exception. Handling the exception follows the same route as using a
try...except
statement. However, the problem is that the exception you get isn’t the exception you raised.
Look at Figure 12-16 and you see that the
ArgumentException
becomes a
ValueError
and the
ArithmeticException
becomes an
ArithmeticError
.
USINg IRoNPyTHoN CoNSTRUCTIVEly
This chapter has shown you some practical techniques for debugging your IronPython application.
In some respects, IronPython actually makes things easier for you by forcing you to use good coding
practice to write your applications. Sloppy programming will cost you so much time as to make the
programming experience a nightmare. Using a combination of warnings, error trapping, and excep-
tions will make your code significantly easier to debug. Of course, choosing the right debugging tool
is also a requirement if you want to go home this weekend, rather than spending it in your office
debugging your latest application.
548592c12.indd 266
2/24/10 12:48:51 PM
www.finebook.ir
Using IronPython Constructively
❘
267
FIgURE 12-16:
IronPython tends to change the name of .NET exceptions.
You’ll get plenty of practice debugging applications. Even experts create applications that don’t
behave as expected. However, sometimes it’s educational to create an application that you use to
experiment. Write the application, make sure it doesn’t actually contain any bugs, and then introduce
bugs to see how various tools work in finding them. Working with known bugs can help you discover
what unknown bugs look like based on their pattern. Fixing known bugs also helps you discover new
techniques for handling bugs so that you can locate and repair bugs in production applications faster.
Chapter 13 begins a new part of the book. In this part, you begin working through some advanced
IronPython capabilities — some of which you can also perform in Python (such as reading and writ-
ing XML), and some that might prove difficult when working with Python (such as interacting with
another .NET language). Chapter 13 begins simply by working through the intricacies of XML
development. You’ll find that IronPython provides many methods of working with XML. Of course,
you might find uses for the debugging techniques covered in this chapter as you work through these
advanced IronPython topics.
548592c12.indd 267
2/24/10 12:48:51 PM
www.finebook.ir
548592c12.indd 268
2/24/10 12:48:51 PM
www.finebook.ir
Do'stlaringiz bilan baham: |