[Python-ideas] Idea
Chris Angelico
rosuav at gmail.com
Fri Aug 22 02:37:31 CEST 2014
On Fri, Aug 22, 2014 at 5:23 AM, Scott Blair <scottyblair at gmail.com> wrote:
> Add continue to a try, except statement to continue where the code threw an
> error. Or a function of exceptions to get the line number at which it was
> thrown along with a way to go back to that line.
You can get the line number from the traceback. However, I don't think
it's a good idea *in the general case* to jump back to the line that
started a problem, because you have no way of undoing anything that
got half done (Python is not transactional). But if you have a
specific situation in which you want to do this, chances are you can
wrap stuff up into a function. For instance, here's something that
would reasonably want a "RESUME" statement (like in BASIC):
try:
install_file("base.py")
install_file("subdir/foo.py")
install_file("subdir/bar.py")
install_file("otherdir/spam.py")
install_file("otherdir/ham.py")
except PathNotFoundError:
create_directory(whichever_one_is_missing)
resume
But this is better served by a simple wrapper:
def install_and_create_dir(dir,fn):
while "infinite retry"
try:
install_file(dir+"/"+fn)
return
except PathNotFoundError:
create_directory(dir)
install_file("base.py")
install_and_create_dir("subdir","foo.py")
install_and_create_dir("subdir","bar.py")
install_and_create_dir("otherdir","spam.py")
install_and_create_dir("otherdir","ham.py")
Do you have a concrete use-case that begs for a RESUME statement and
can't easily be refactored into a (possibly nested) function?
ChrisA
More information about the Python-ideas
mailing list