467,912 Members | 1,517 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,912 developers. It's quick & easy.

confusion about Exception Mechanism

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
Jul 18 '05 #1
  • viewed: 1090
Share:
3 Replies
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/>
Jul 18 '05 #2
>>>>> "Zachary" == Zachary <Z_*****@hotmail.com> 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.

Jul 18 '05 #3
| 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?"""

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

42 posts views Thread by cody | last post: by
28 posts views Thread by Scott Brady Drummonds | last post: by
3 posts views Thread by Master of C++ | last post: by
4 posts views Thread by Robbie Hatley | last post: by
4 posts views Thread by Anirudha Vengurlekar | last post: by
44 posts views Thread by craig | last post: by
132 posts views Thread by Zorro | last post: by
1 post views Thread by George2 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.