newb: Creating Exception

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

    newb: Creating Exception

    I want to print individual exception for database connection, sql
    execution, database closing, closing the cursor. Can I do it with one
    try..catch or I need a nested try...catch?

    conn = adodb.NewADOCon nection('mysql' )
    conn.Connect('l ocalhost', 'temp', 'temp', 'temp')

    sql = r"update file set pdf_file_path=' " +finalFile+r"' where
    file_path='"+p+ r"'"


    cursor = conn.Execute(sq l)
    rows = cursor.Affected _Rows()

    cursor.close()
    conn.close()

    Thank you for your FOUNTAIN OF WISDOM...

  • johnny

    #2
    Re: newb: Creating Exception

    Thank you Dennis,
    So when line 2, gets executed, its exception goes to do_some1_error.
    And when line 3, gets executed, its exception goes to do_some2_error
    and so on.

    line 1: try
    line 2: do_some1
    line 3: do_some2
    line 4: do_some3
    line 5: except do_some1_error:
    line 6: whatever1
    line 7: except do_some2_error:
    line 8: whatever2
    line 9: except do_some3_error:
    line 10: whatever3

    Documentation is not written for newbs, it's written by guys with 6yrs
    of experience FOR guys with 6yrs of experience.

    Dennis Lee Bieber wrote:
    On 11 Dec 2006 16:02:02 -0800, "johnny" <rampeters@gmai l.comdeclaimed
    the following in gmane.comp.pyth on.general:
    >
    I want to print individual exception for database connection, sql
    execution, database closing, closing the cursor. Can I do it with one
    try..catch or I need a nested try...catch?
    >
    Python does not have a "catch" instruction.
    >
    You could do:
    >
    try:
    make connection #though that should, in my mind, be done
    #as part of the initialization of the thread
    #and not as part of any processing loop
    make cursor
    execute sql
    fetch results if any
    close cursor
    commit transaction
    close connection #which I'd make part of the termination
    #of the thread
    except Exception1, msg:
    do something
    except Exception2, msg:
    do something2
    ...
    >
    IF each step raises a different exception type -- if all the database
    returns is "DatabaseError" , then there is nothing to separate them by.
    Also note that if an exception happens in the "execute sql" stage, your
    handler may need to do a rollback, and the closes.
    >
    --
    Wulfraed Dennis Lee Bieber KD6MOG
    wlfraed@ix.netc om.com wulfraed@bestia ria.com

    (Bestiaria Support Staff: web-asst@bestiaria. com)
    HTTP://www.bestiaria.com/

    Comment

    • Dustan

      #3
      Re: newb: Creating Exception


      johnny wrote:
      Thank you Dennis,
      So when line 2, gets executed, its exception goes to do_some1_error.
      And when line 3, gets executed, its exception goes to do_some2_error
      and so on.
      >
      line 1: try
      line 2: do_some1
      line 3: do_some2
      line 4: do_some3
      line 5: except do_some1_error:
      line 6: whatever1
      line 7: except do_some2_error:
      line 8: whatever2
      line 9: except do_some3_error:
      line 10: whatever3
      >
      Documentation is not written for newbs, it's written by guys with 6yrs
      of experience FOR guys with 6yrs of experience.
      You might want to get a book on python, rather than depend on the
      documentation, which is, as you say, written for more experienced
      programmers.



      I started with a book, and reading the tutorial now, am quite glad I
      did. One thing that did bug me, at least briefly, is sometimes beginner
      books don't explain what a line of code is actually doing - not
      necessarily how it works, but as much information as is necessary to
      actually be able to use the code shown.
      Dennis Lee Bieber wrote:
      On 11 Dec 2006 16:02:02 -0800, "johnny" <rampeters@gmai l.comdeclaimed
      the following in gmane.comp.pyth on.general:
      I want to print individual exception for database connection, sql
      execution, database closing, closing the cursor. Can I do it with one
      try..catch or I need a nested try...catch?
      Python does not have a "catch" instruction.

      You could do:

      try:
      make connection #though that should, in my mind, be done
      #as part of the initialization of the thread
      #and not as part of any processing loop
      make cursor
      execute sql
      fetch results if any
      close cursor
      commit transaction
      close connection #which I'd make part of the termination
      #of the thread
      except Exception1, msg:
      do something
      except Exception2, msg:
      do something2
      ...

      IF each step raises a different exception type -- if all the database
      returns is "DatabaseError" , then there is nothing to separate them by.
      Also note that if an exception happens in the "execute sql" stage, your
      handler may need to do a rollback, and the closes.

      --
      Wulfraed Dennis Lee Bieber KD6MOG
      wlfraed@ix.netc om.com wulfraed@bestia ria.com

      (Bestiaria Support Staff: web-asst@bestiaria. com)
      HTTP://www.bestiaria.com/

      Comment

      • Dustan

        #4
        Re: newb: Creating Exception


        Dustan wrote:
        johnny wrote:
        Documentation is not written for newbs, it's written by guys with 6yrs
        of experience FOR guys with 6yrs of experience.
        >
        You might want to get a book on python, rather than depend on the
        documentation, which is, as you say, written for more experienced
        programmers.
        >

        >
        I started with a book, and reading the tutorial now, am quite glad I
        did. One thing that did bug me, at least briefly, is sometimes beginner
        books don't explain what a line of code is actually doing - not
        necessarily how it works, but as much information as is necessary to
        actually be able to use the code shown.
        I didn't complete my thought. If you run into a situation like this,
        then you might want to look to the python documentation on the web for
        help; think of the web documentation as a reference manual rather than
        a tutorial, even though it does provide a tutorial.

        Comment

        • Dustan

          #5
          Re: newb: Creating Exception

          Dennis Lee Bieber wrote:
          On 13 Dec 2006 03:52:49 -0800, "Dustan" <DustanGroups@g mail.com>
          declaimed the following in gmane.comp.pyth on.general:
          >

          I didn't complete my thought. If you run into a situation like this,
          then you might want to look to the python documentation on the web for
          help; think of the web documentation as a reference manual rather than
          a tutorial, even though it does provide a tutorial.
          >
          I believe it was stated that the installation was using the
          ActiveState build. The documentation is supplied in Windows CHM format,
          which is likely much faster to search/read locally than loading a chain
          of web pages.
          I couldn't think of a better word to describe the 'official'
          documentation.
          Including, last time I checked, an electronic copy of "Dive into
          Python"

          Comment

          • johnny

            #6
            Re: newb: Creating Exception

            I checked out couple of books from Library, one that I like called
            "Foundation of Python Network Programming", this is what I needed, code
            with understandable explantion. ;) Thread pooling and many others.
            Thanks all of you for the help.


            Dustan wrote:
            Dennis Lee Bieber wrote:
            On 13 Dec 2006 03:52:49 -0800, "Dustan" <DustanGroups@g mail.com>
            declaimed the following in gmane.comp.pyth on.general:
            >
            I didn't complete my thought. If you run into a situation like this,
            then you might want to look to the python documentation on the web for
            help; think of the web documentation as a reference manual rather than
            a tutorial, even though it does provide a tutorial.
            I believe it was stated that the installation was using the
            ActiveState build. The documentation is supplied in Windows CHM format,
            which is likely much faster to search/read locally than loading a chain
            of web pages.
            >
            I couldn't think of a better word to describe the 'official'
            documentation.
            >
            Including, last time I checked, an electronic copy of "Dive into
            Python"

            Comment

            Working...