what is the reasonable (best?) Exception handling strategy?

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

    #1

    what is the reasonable (best?) Exception handling strategy?

    I am a little bit confused by all possibilities for exceptions handling
    in Python (probably because I am not skilled enough??) I did try to
    search trough this list and reading Python tutorial about Errors and
    Exceptions but didn't find some "general" answer about exception
    handling policy (strategy).

    In the following example each row can IMHO raise an exception (if the
    Firebird service is not running for example, if the database is
    corrupted etc.).

    Do I have to write "try/except" clause on each row?

    Or to write try/except block (function) where to handle (on one place)
    all exceptions expected in the program code is a good idea?

    Or do I have to write own "exception hook"?

    What about unexpected exceptions? :(

    def databasExample( h,d,u,p):
    import kinterbasdb; kinterbasdb.ini t(type_conv=200 )
    con = kinterbasdb.con nect(host=h, database=d,user =u, password=p)
    cur = con.cursor()
    insertStatement = cur.prep("some SQL statement...... ")
    cur.executemany (insertStatemen t, ListOfValues)
    con.commit()
    cur.close()

    Generally I am trying to find some general advices or suggestions about
    exception handling more than the specific answers to the above
    mentioned code example.

    Regards

    Petr Jakes

  • Duncan Booth

    #2
    Re: what is the reasonable (best?) Exception handling strategy?

    Petr Jakes wrote:
    [color=blue]
    > I am a little bit confused by all possibilities for exceptions handling
    > in Python (probably because I am not skilled enough??) I did try to
    > search trough this list and reading Python tutorial about Errors and
    > Exceptions but didn't find some "general" answer about exception
    > handling policy (strategy).[/color]

    It depends on what you are actually able to do about the exception. If you
    can recover from it meaningfully then you may want to handle it near the
    place it is thrown. If all you can do is abort the entire program then you
    handle that at the outermost level of the program.
    [color=blue]
    >
    > In the following example each row can IMHO raise an exception (if the
    > Firebird service is not running for example, if the database is
    > corrupted etc.).[/color]

    If a service isn't running that sounds pretty fatal. Handle it at the outer
    levels of your code. If the database is corrupted that might also be
    terminal unless you include bad data (e.g. invalid email address) in that
    definition, in that case it may be something you can fix, ignore, or live
    with: it should be obvious in this case where in your code you need to do
    the fixup or ignoring.
    [color=blue]
    >
    > Do I have to write "try/except" clause on each row?[/color]

    The processing you perform on a row might raise an exception for which the
    correct action would be to simply continue with the next row. In that case
    handle the exception inside the 'processRow' function so the code which
    iterates over the rows never sees it. If it is a more serious problem which
    is going to stop you processing any further rows then you let it propogate.
    [color=blue]
    >
    > Or to write try/except block (function) where to handle (on one place)
    > all exceptions expected in the program code is a good idea?
    >
    > Or do I have to write own "exception hook"?
    >
    > What about unexpected exceptions? :([/color]

    Big errors, or unexpected errors you handle in one place usually by making
    sure a human is alerted to the problem.

    Comment

    • Rene Pijlman

      #3
      Re: what is the reasonable (best?) Exception handling strategy?

      Petr Jakes:[color=blue]
      >What about unexpected exceptions? :([/color]

      I asked a similar question some time ago:


      --
      René Pijlman

      Comment

      Working...