Author kevincox
Recipients kevincox
Date 2014-03-25.22:44:17
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1395787457.4.0.287090998616.issue21067@psf.upfronthosting.co.za>
In-reply-to
Content
I think it would be useful to support multiple finally clauses.  The idea would be that each clause would be run, even if prior clauses throw exceptions.

The idea came when hunting a bug in the Mozilla test suite.  The code looked like as follows.

try:
	resource1 = allocateresource1()
	resource2 = allocateresource2()
	
	dostuffthatmightthrowexception()
finally:
	if resource1:
		resource1.close()
	if resource2:
		resource2.close()

The problem is that if resource1,close() throws an exception resource2 is not closed.

The alternative looks like this.

try:
	resource1 = allocateresource1()
	try:
		resource2 = allocateresource2()
		
		dostuffthatmightthrowexception()
	finally:
		if resource2:
			resource2.close()
finally:
	if resource2:
		resource2.close()

Or it could look like this.

try:
	resource1 = allocateresource1()
	resource2 = allocateresource2()
	
	dostuffthatmightthrowexception()
finally:
	try:
		if resource1:
			resource1.close()
	finally:
		if resource2:
			resource2.close()

Both of which exhibit indentation explosion when there are a number of resources that need to be cleaned up.

If multiple finally clauses were allowed the code would be much more readable and would look as follows.

try:
	resource1 = allocateresource1()
	resource2 = allocateresource2()
	
	dostuffthatmightthrowexception()
finally:
	if resource1:
		resource1.close()
finally:
	if resource2:
		resource2.close()
History
Date User Action Args
2014-03-25 22:44:17kevincoxsetrecipients: + kevincox
2014-03-25 22:44:17kevincoxsetmessageid: <1395787457.4.0.287090998616.issue21067@psf.upfronthosting.co.za>
2014-03-25 22:44:17kevincoxlinkissue21067 messages
2014-03-25 22:44:17kevincoxcreate