473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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
3 1251
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Bill Davidson | last post by:
Hi there, Please forgive me for posting this article on multiple groups. Being new in the newsgroups, I was not sure which group would have been appropriate for my question. Sorry. My...
42
by: cody | last post by:
public DateTime Value { get { try { return new DateTime(int.Parse(tbYear.Text), int.Parse(tbMonth.Text), int.Parse(tbDay.Text)); } catch (FormatException)
28
by: Scott Brady Drummonds | last post by:
Hi, all, I just got out of a meeting with a team of software developers that I recently joined as they are staffing to create a medium-sized project (potentially all of which will be written in...
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
4
by: Robbie Hatley | last post by:
I've been playing with "auto_ptr" and the "Resource Acquisition Is Initialization" concept. On page 199 of Lippman's book "Effective C++" he says "All active local class objects of a function are...
4
by: Anirudha Vengurlekar | last post by:
Hello Community, We can have nested TRY , CATCH , FINALLY blocks in the application. So in what situation (in a class or in a function) I would use this nested Try catch blocks? If someone...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.