[Tutor] Please help to debug this function..
ALAN GAULD
alan.gauld at btinternet.com
Tue Nov 7 00:02:39 CET 2006
Reposting to the list.
--- Asrarahmed Kadri <ajkadri at googlemail.com> wrote:
> > > except ValueError:
> > > flag = False
> > > startdate = None
> > > enddate = None
> > > err_msg = traceback.format_exc()
> > > index = string.find(err_msg,'Value')
> > > print err_msg[index:]
> > > return (flag,startdate,enddate)
> >
> >Since this block is essentially identical to the except
> >block above you could package them both as a function
> >which would shorten the code a little.
> It might seem silly, but can you just give a hint as to how to
> put the code in a function..
def handleValueError():
flag = False
startdate = None
enddate = None
err_msg = traceback.format_exc()
index = err_msg.find('Value')
print err_msg[index:]
return (flag,startdate,enddate)
And in your code above:
except ValueError:
return handleValueError()
> > It would be better to raise a ValueError which can be caught
> > by the external program:
> How to catch the exception from one module into another ..???
Just use try/except as usual. There is no difference
to exceptions thrown by your code and the ones thrown
by the standard Python functions. Consider:
####### module.py #######
def f():
raise ValueError
##########################
###### main.py ##########
import module
try:
module.f()
except ValueError:
print "It broke!"
#########################
Is that clear?
Alan G
___________________________________________________________
All new Yahoo! Mail "The new Interface is stunning in its simplicity and ease of use." - PC Magazine
http://uk.docs.yahoo.com/nowyoucan.html
More information about the Tutor
mailing list