__init__ method and raising exceptions

N

NavyJay

I have a simple for-loop, which instantiates a class object each
iteration. As part of my class constructor, __init__(), I check for
valid input settings. If there is a problem with this iteration, I
want to abort the loop, but equivalently 'continue' on in the for-loop.

I can't use 'break' or 'continue' in a class method, nor can I return a
boolean value from __init__() to check for errors within the for-loop.
How would I be able to stop the current iteration and continue with the
next after reporting an error?

Jay
 
L

Leif K-Brooks

NavyJay said:
I have a simple for-loop, which instantiates a class object each
iteration. As part of my class constructor, __init__(), I check for
valid input settings. If there is a problem with this iteration, I
want to abort the loop, but equivalently 'continue' on in the for-loop.

I can't use 'break' or 'continue' in a class method, nor can I return a
boolean value from __init__() to check for errors within the for-loop.
How would I be able to stop the current iteration and continue with the
next after reporting an error?

You have the right idea in the subject header: raise an exception.
Something along the lines of this:

class Foo(object):
def __init__(self, value):
if value > 10:
raise ValueError("Value must be under 10, was %s." % value)
else:
self.value = value

for value in [1, 4, 10, 7, 15, 13, 6, 3]:
try:
obj = Foo(value)
except ValueError, ex:
print str(ex)
continue
# Do stuff with obj here
 
V

Vikram

I can't use 'break' or 'continue' in a class method, nor can I return a
boolean value from __init__() to check for errors within the for-loop.
How would I be able to stop the current iteration and continue with the
next after reporting an error?

maybe i don't fully understand your qn but why don't you let __init__ of
your class raise an exception and then catch it in the outer for loop and
continue. something like:

for i in ...
try:
foo()
except:
continue

Vikram
 
N

NavyJay

Exactly the answer I was looking for! 12 hours of straight programming
tends to fog ones mind. Thanks for making it clear!

Jay
 
S

Scott David Daniels

Vikram said:
maybe i don't fully understand your qn but why don't you let __init__ of
your class raise an exception and then catch it in the outer for loop and
continue. something like:

for i in ...
try:
foo()
except:
except (ValueError, TypeError), error:

Use something like the above, (always expect certain kinds of errors)
lest you accidentally capture an real attempt to stop the program
such as the exception a Control-C causes.

-Scott David Daniels
(e-mail address removed)
 
N

NavyJay

Or better yet, define your own string/class exception to catch your
errors. In my code, things can break in more than a few ways. In each
case I catch the exception(s) specific to that piece of code, print a
warning message to the user at sys.stdout and raise a new exception to
be caught by my "wrapper" exception clause.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top