[Tutor] constructors

alan.gauld@bt.com alan.gauld@bt.com
Fri, 12 Apr 2002 13:59:50 +0100


> I see.  Throwing an exception is a more flexible way of 
> "erroring-out", but it requires more work to code 
> an appropriate response if you want to 
> take advantage of this flexibility.

Not much more in practice.

errCode = operation(foo)
if errCode == ERROR_1:
   # do something
elif errCode == ERROR_2:
   # do another
else: print "unexpected error , errCode, " occured"

try: operation(foo)
except ERROR_1: 
	# do something
except ERROR_2:
	# do another
except: print "Unexpected error "

Apart from typing raise ERROR_1 instead of return ERROR_1
the originating code isn't much doifferent either.

The only snag is that sometimes you can lose context 
which makes it harder to recover from an exception 
that a return valued error. In that case you wind up 
with a lot of

try: doit()
except: #recover here

two line pairs, but again no worse than explicit return 
value checks.

Just my 2 cents worth...

Alan g.