By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,171 Members | 809 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,171 IT Pros & Developers. It's quick & easy.

error handling

P: n/a
I want to handle errors for a program i'm building in a specific way,
but I don't want to use try/except/finally because it requires forming
new blocks of code. I want to be able things like this:

a = [2, 423, "brownie", 234.34]
try: a[890]
except IndexError: # I don't use 'finally' here because I specifically
want to check for an IndexError
sys.exit("That number is way too big!")

But sometimes you can have too many of these statements in your
program, and it starts to get tangled and nasty looking. Is there a way
I can modify sys.error so that when the interpreter comes accross an
IndexError it prints "That number is way too big!" before it exits?

Aug 10 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Chris wrote:
But sometimes you can have too many of these statements in your
program, and it starts to get tangled and nasty looking. Is there a way
I can modify sys.error so that when the interpreter comes accross an
IndexError it prints "That number is way too big!" before it exits?
Hi,

Try placing the try/except block around the main function of your program:

if __name__ == '__main__':
try:
main()
except IndexError:
sys.exit("That number is way too big!")

-Farshid
Aug 10 '06 #2

P: n/a
Chris wrote:
I want to handle errors for a program i'm building in a specific way,
but I don't want to use try/except/finally because it requires forming
new blocks of code. I want to be able things like this:

a = [2, 423, "brownie", 234.34]
try: a[890]
except IndexError: # I don't use 'finally' here because I specifically
want to check for an IndexError
sys.exit("That number is way too big!")

But sometimes you can have too many of these statements in your
program, and it starts to get tangled and nasty looking. Is there a way
I can modify sys.error so that when the interpreter comes accross an
IndexError it prints "That number is way too big!" before it exits?
You don't want to do what because why? Huh?

I'm sorry man, I don't want to be harsh, but use try..except blocks.

If your code is getting "tangled and nasty looking" you probably need
to break it up into small[er] functions, or redesign it somehow.

Peace,
~Simon

(Also, there's no such thing as sys.error, do you mean
sys.excepthook()?)

Aug 10 '06 #3

P: n/a
I want to do this because there are several spots in my program where
an error might occur that I want to handle the same way, but I don't
want to rewrite the try..except block again. Is that clearer?

And I meant sys.stderr... sorry 'bout that

Simon Forman wrote:
Chris wrote:
I want to handle errors for a program i'm building in a specific way,
but I don't want to use try/except/finally because it requires forming
new blocks of code. I want to be able things like this:

a = [2, 423, "brownie", 234.34]
try: a[890]
except IndexError: # I don't use 'finally' here because I specifically
want to check for an IndexError
sys.exit("That number is way too big!")

But sometimes you can have too many of these statements in your
program, and it starts to get tangled and nasty looking. Is there a way
I can modify sys.error so that when the interpreter comes accross an
IndexError it prints "That number is way too big!" before it exits?

You don't want to do what because why? Huh?

I'm sorry man, I don't want to be harsh, but use try..except blocks.

If your code is getting "tangled and nasty looking" you probably need
to break it up into small[er] functions, or redesign it somehow.

Peace,
~Simon

(Also, there's no such thing as sys.error, do you mean
sys.excepthook()?)
Aug 11 '06 #4

P: n/a

Chris wrote:
I want to handle errors for a program i'm building in a specific way,
but I don't want to use try/except/finally because it requires forming
new blocks of code. I want to be able things like this:

a = [2, 423, "brownie", 234.34]
try: a[890]
except IndexError: # I don't use 'finally' here because I specifically
want to check for an IndexError
sys.exit("That number is way too big!")

But sometimes you can have too many of these statements in your
program, and it starts to get tangled and nasty looking. Is there a way
I can modify sys.error so that when the interpreter comes accross an
IndexError it prints "That number is way too big!" before it exits?
Call me crazy, but I don't think convoluted mucking about to produce
"That number is way too big!" -- especially when it could be negative
(way too small) -- is better than doing nothing (no try/except at all)
and getting "list index out of range":

#>>a = [1,42, 666]
#>>a[890]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IndexError: list index out of range
#>>>

Although the manual says "In particular, sys.exit("some error message")
is a quick way to exit a program when an error occurs." this is
scarcely to be recommended. You should at the very least produce a
stack trace so that you can see *where* the error occurred.

<rant>
If you are interacting with a user, you need to give feedback for
*expected* problems like invalid input as close to the point of error
as possible -- online: validate the input before attempting to use it;
batch -- validate it and give feedback which pinpoints the problem (and
it may be required to identify all problems, not just bail out on
seeing the first puff of flak). If it is for your own use: do yourself
a favour and treat yourself like a customer should be treated. You
should have traps for *unexpected* exceptions that likewise pinpoint
what was happening when the exception occurred.

Consider dumping salient info to a file before pulling the ripcord --
that way you don't have to rely on users reporting what they thought
they saw before hitting the power button. Time passing has not meant
progress; user memory capacity has not increased and the usual
cycle-power response time is worse; rebooting a PC takes way longer
than rebooting a green-screen terminal :-)
</rant>

HTH,
John

Aug 11 '06 #5

P: n/a
On Thu, 10 Aug 2006 16:59:56 -0700, Farshid Lashkari wrote:
Chris wrote:
>But sometimes you can have too many of these statements in your
program, and it starts to get tangled and nasty looking. Is there a way
I can modify sys.error so that when the interpreter comes accross an
IndexError it prints "That number is way too big!" before it exits?

Hi,

Try placing the try/except block around the main function of your program:

if __name__ == '__main__':
try:
main()
except IndexError:
sys.exit("That number is way too big!")

That's broken.

Imagine that somewhere in main() the following is called:

D = {"a": "apple", "b": "bicycle", "c": "cat"}
print D["aardvark"]

Your code now prints "That number is way too big!". That's not good.

try...except blocks should, as a general rule, cover only the smallest
amount of code that they need to.

One possible solution is to use a custom class:

# untested
class MyList(list):
def __getitem__(self, i):
if i >= len(self):
raise IndexError("That index is too big!")
super(list, self).__getitem__(i)

Another is a helper function:

def safe_getindex(somelist, i):
if i >= len(somelist):
raise IndexError("That index is too big!")
return somelist[i]
You can of course re-write them to use a try...except block instead of
testing the index.
--
Steven D'Aprano

Aug 11 '06 #6

P: n/a
In article <11*********************@74g2000cwt.googlegroups.c om>,
Chris <ch*********@gmail.comwrote:
>I want to do this because there are several spots in my program where
an error might occur that I want to handle the same way, but I don't
want to rewrite the try..except block again. Is that clearer?
Aug 11 '06 #7

P: n/a
Steven D'Aprano wrote:
That's broken.

Imagine that somewhere in main() the following is called:

D = {"a": "apple", "b": "bicycle", "c": "cat"}
print D["aardvark"]

Your code now prints "That number is way too big!". That's not good.

try...except blocks should, as a general rule, cover only the smallest
amount of code that they need to.
Hi Steven,

Point taken and I think your solution better addresses the root of the
problem. However, the OP said he wanted to print out that error message
whenever the interpreter came across any IndexError. So I gave him what
he wanted. I guess he needs to be more careful what he wishes for ;)

-Farshid
Aug 11 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.