Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #100396
| X-Received | by 10.141.28.206 with SMTP id f197mr19506328qhe.8.1450041004279; Sun, 13 Dec 2015 13:10:04 -0800 (PST) |
|---|---|
| X-Received | by 10.50.117.39 with SMTP id kb7mr7738igb.8.1450041004243; Sun, 13 Dec 2015 13:10:04 -0800 (PST) |
| Path | csiph.com!au2pb.net!feeder.erje.net!2.us.feeder.erje.net!news.glorb.com!g67no647831qgd.1!news-out.google.com!l1ni2397igd.0!nntp.google.com!mv3no15710209igc.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
| Newsgroups | comp.lang.python |
| Date | Sun, 13 Dec 2015 13:10:03 -0800 (PST) |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=24.63.27.64; posting-account=Jod8CwoAAAD06WqLVFnlf8nIDonFoRWK |
| NNTP-Posting-Host | 24.63.27.64 |
| User-Agent | G2/1.0 |
| MIME-Version | 1.0 |
| Message-ID | <b976659a-a7e0-40b4-b74b-2b3689e64d5c@googlegroups.com> (permalink) |
| Subject | Why is break allowed in finally, but continue is not? |
| From | Ned Batchelder <ned@nedbatchelder.com> |
| Injection-Date | Sun, 13 Dec 2015 21:10:04 +0000 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Xref | csiph.com comp.lang.python:100396 |
Show key headers only | View raw
For testing coverage.py, I wrote a program to generate
randomly-structured Python functions. When compiling
the results, I got a message I'd never seen before:
SyntaxError: 'continue' not supported inside 'finally' clause
I guess this makes sense, when cleaning up from an
exception, continuing the loop seems an odd thing to do.
But 'break' is allowed in 'finally' clauses! I tried this:
# Huh? This prints "here", and no exception is raised.
for i in range(1):
try:
1/0
finally:
# If you change this to "continue", you get:
# 'continue' not supported inside 'finally' clause
break
print "here"
The finally is perfectly willing to have a 'break', but it's
a syntax error to have 'continue'? Why? I don't see a
difference between the two when it comes to cleaning up
exceptions.
There are other things you can do in a finally clause that
will prevent the exception from being raised, like 'return',
and 'break' works just fine.
So why treat 'continue' specially?
--Ned.
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Why is break allowed in finally, but continue is not? Ned Batchelder <ned@nedbatchelder.com> - 2015-12-13 13:10 -0800
Re: Why is break allowed in finally, but continue is not? Ben Finney <ben+python@benfinney.id.au> - 2015-12-14 09:27 +1100
Re: Why is break allowed in finally, but continue is not? Ned Batchelder <ned@nedbatchelder.com> - 2015-12-14 09:37 -0800
Re: Why is break allowed in finally, but continue is not? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-14 18:40 +0000
csiph-web