confusion about Exception Mechanism

Discussion in 'Python' started by Ben Finney, Jan 15, 2004.

  1. Ben Finney

    Ben Finney Guest

    On Wed, 14 Jan 2004 17:31:41 -0800, Zachary wrote:
    > I'm relatively new at Python


    Welcome! Please work your way through the tutorial, to get a good
    grounding in the language.

    <http://www.python.org/doc/current/tut/>

    > slightly confused about how the try...except mechanism is supposed to
    > work.


    Chapter 8 of the tutorial deals with errors and exceptions.

    > I am just not sure how it would
    > be used, as it just seems that it is another sort of loop.


    Goodness, that *is* confused :)

    > Is their any sort of example I could look at, that has an example of
    > how it is applied?


    Work your way through the tutorial, start to finish, and see if it
    doesn't also answer a whole lot of other questions -- including ones you
    haven't thought of yet.

    --
    \ "Faith may be defined briefly as an illogical belief in the |
    `\ occurrence of the improbable." -- Henry L. Mencken |
    _o__) |
    Ben Finney <http://bignose.squidly.org/>
     
    Ben Finney, Jan 15, 2004
    #1
    1. Advertising

  2. Ben Finney

    Zachary Guest

    Hello,
    I'm relatively new at Python, and am slightly confused about how the
    try...except mechanism is supposed to work. I am just not sure how it would
    be used, as it just seems that it is another sort of loop. Is their any
    sort of example I could look at, that has an example of how it is applied?
    I haven't really seen any such full example.
    Thanks, And Sorry for the newbie Question,
    Zack
     
    Zachary, Jan 15, 2004
    #2
    1. Advertising

  3. Ben Finney

    Isaac To Guest

    >>>>> "Zachary" == Zachary <> writes:

    Zachary> Hello, I'm relatively new at Python, and am slightly confused
    Zachary> about how the try...except mechanism is supposed to work. I am
    Zachary> just not sure how it would be used, as it just seems that it is
    Zachary> another sort of loop. Is their any sort of example I could
    Zachary> look at, that has an example of how it is applied? I haven't
    Zachary> really seen any such full example.

    How you think that exception is a kind of loop? E.g., documentation where
    you get that idea? If any documentation leads you to think that way, the
    documentation must be clarified (e.g., by modifying it).

    Regards,
    Isaac.
     
    Isaac To, Jan 15, 2004
    #3
  4. | Zachary said |

    > Hello,
    > I'm relatively new at Python, and am slightly confused about how the
    > try...except mechanism is supposed to work. I am just not sure how it
    > would be used, as it just seems that it is another sort of loop. Is their
    > any sort of example I could look at, that has an example of how it is
    > applied? I haven't really seen any such full example. Thanks, And Sorry
    > for the newbie Question, Zack


    When I first encountered exceptions, they were a bit confusing for me too.

    Exceptions are failsafes that allow a program to sensibly deal with
    errors. When something goes wrong, Python won't just spit out an error
    message. First it "raises an exception." You may also hear people call
    that throwing an exception. A try block allows you to "catch" that
    exception and deal with it yourself. In a way, it's the program sending
    out a cry for help. By putting something in a try block, you're saying
    that you're willing to answer that cry.

    For instance: (stolen from the tutorial page)

    import sys

    try:
    f = open('myfile.txt')
    s = f.readline()
    i = int(s.strip())
    except ValueError:
    print "Could not convert data to an integer."
    except IOError, (errno, strerror):
    print "I/O error(%s): %s" % (errno, strerror)
    except:
    print "Unexpected error:", sys.exc_info()[0]
    raise


    You put the code you're willing to handle exceptions (errors) for between
    try and except. The first except (there only needs to be one, there may
    be multiple) says that if the block of code encounters a "ValueError" problem,
    that we will print "Could not convert data to an integer." and then keeps
    moving along in the program. The second except block handles IOError
    exceptions, and also gets some extra information from those exceptions.
    It stores this extra information in "errno" and "strerror". It then
    prints some information about what exactly went wrong and continues
    running the program. The last except block catches any and every error
    that wasn't caught before it. It will print an error message, then the
    statement "raise" falls back on the next line of defense of errors. Since
    this bit of code may itself be inside a try block, maybe the program will
    handle it elsewhere. If there's no other try blocks to handle an
    exception, then, and only then, will Python spit out an error message and
    stop the program.

    There's other pieces to this puzzle, but they're all on the tutorial page
    pointed out elsewhere. Hopefully this will give you some sense of bearing
    while reading that.

    HTH

    Sam Walters.

    --
    Never forget the halloween documents.
    http://www.opensource.org/halloween/
    """ Where will Microsoft try to drag you today?
    Do you really want to go there?"""
     
    Samuel Walters, Jan 15, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. piyush
    Replies:
    0
    Views:
    1,876
    piyush
    Jul 14, 2004
  2. Replies:
    3
    Views:
    378
    Ben Hutchings
    Dec 13, 2005
  3. Thea
    Replies:
    6
    Views:
    2,072
  4. Ralph A. Moeritz

    exception handling mechanism

    Ralph A. Moeritz, Apr 12, 2005, in forum: C Programming
    Replies:
    1
    Views:
    350
    Fred L. Kleinschmidt
    Apr 12, 2005
  5. Peng Yu
    Replies:
    95
    Views:
    1,551
    Roel Schroeven
    Jan 7, 2010
Loading...

Share This Page