Thread overview
BUG: finally block executes multiple times
Nov 16, 2004
Sean Kelly
Nov 17, 2004
Thomas Kuehne
Nov 17, 2004
Sean Kelly
November 16, 2004
Executing the following code:

void main()
{
try
{
try
{
printf( "hello\n" );
throw new Exception( "first" );
}
finally
{
printf( "finally\n" );
throw new Exception( "second" );
}
}
catch( Exception e )
{
printf( "catch\n" );
e.print();
}
}

prints:

hello
finally
finally
.. (maybe 100 iterations)
finally


November 17, 2004
This version of the problem seems to be Windows-only. On Linux I get:
# hello
# finally
# Error: second

error.html reads:
# Finally blocks are executed as the stack is unwound. If an error handler
# is found, execution resumes there. If not, the default Error handler is
# run, which displays the message and terminates the program.

Due to "catch( Exception e )" I would expect the output:
# helo
# finally
# catch
# Error: second

Sean Kelly schrieb am Tue, 16 Nov 2004 20:23:32 +0000 (UTC):
> Executing the following code:
>
> void main() {
>	try{
>		try{
>			printf( "hello\n" );
>			throw new Exception( "first" );
>		}finally{
>			printf( "finally\n" );
>			throw new Exception( "second" );
> 		}
> 	} catch( Exception e ) {
>		printf( "catch\n" );
>		e.print();
> 	}
> }
>
> prints:
>
> hello
> finally
> finally
> .. (maybe 100 iterations)
> finally

Added to DStress as: http://svn.kuehne.cn/dstress/run/finally_01.d

Thomas
November 17, 2004
In article <tgjr62-2vv.ln1@kuehne.cn>, Thomas Kuehne says...
>
>
>This version of the problem seems to be Windows-only. On Linux I get:
># hello
># finally
># Error: second
>
>error.html reads:
># Finally blocks are executed as the stack is unwound. If an error handler
># is found, execution resumes there. If not, the default Error handler is
># run, which displays the message and terminates the program.
>
>Due to "catch( Exception e )" I would expect the output:
># helo
># finally
># catch
># Error: second

I was looking at the code for this yesterday and current behavior is to find the exception handler for the original exception and then to evaluate all the "finally" blocks that were passed.  So I suppose it makes sense that if one of these blocks throws an excecption it would be handled higher up in the program. I've posted a message in the main thread asking about what the correct behavior for this *should* be, as I'm not convinced either is correct.  Still, the Windows bug should be fixed.


Sean