__init__ method and raising exceptions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • NavyJay

    __init__ method and raising exceptions

    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

  • Leif K-Brooks

    #2
    Re: __init__ method and raising exceptions

    NavyJay wrote:[color=blue]
    > 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?[/color]

    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("Val ue 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

    Comment

    • Vikram

      #3
      Re: __init__ method and raising exceptions

      > I can't use 'break' or 'continue' in a class method, nor can I return a[color=blue]
      > 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?[/color]

      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

      Comment

      • NavyJay

        #4
        Re: __init__ method and raising exceptions

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

        Jay

        Comment

        • Scott David Daniels

          #5
          Re: __init__ method and raising exceptions

          Vikram wrote:[color=blue][color=green]
          >>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?[/color]
          >
          >
          > 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:[/color]
          except (ValueError, TypeError), error:[color=blue]
          > continue[/color]

          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
          Scott.Daniels@A cm.Org

          Comment

          • NavyJay

            #6
            Re: __init__ method and raising exceptions

            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.

            Comment

            Working...