Cost of raising exceptions.

Allan Crooks amc1 at dcs.qmw.ac.uk
Tue May 29 09:52:23 EDT 2001


Hi,

I'm fairly new to Python (I fear that's always a bad way to start a
sentence), but rather curious at the same time. Which leads me to this
question:

 Are raising exceptions a costly operation?

Since my real programming experience is in Java, I know that, in Java at
least, throwing an exception generates fairly high overhead. That's why,
for example, the java.util.Iterator interface supplies both a hasNext()
and next() element. The user of the object could repeatedly call the
next() method, and catch the exception when it gets to the end.

But since that has a high overhead, the hasNext() method is a way of
detecting the end. My query is, if developing something similar for
Python, is raising an exception a good way to halt or not?

Allan.





More information about the Python-list mailing list