error handling

Simon Forman rogue_pedro at yahoo.com
Thu Aug 10 20:00:16 EDT 2006


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()?)




More information about the Python-list mailing list