err, actually I prefer the windows under hood behavior. Windows prevents you replace the exception silently.
For a language to fix such a problem would need to have the stacktrace to locate where the exception thrown. It's a bit tough to fix the problem.
Comment #2 by davidl — 2007-09-18T02:39:18Z
if i put another try catch block wrap this , everything goes ok
seems it's not SEH related.
I tried some similar code with C __try __except __try __finally blocks.
It works as expected
I figured it'd been ages and maybe something had changed to maybe fix this bug.. nope:
dmd 1.061 on windows:
Error: ex 2
throw ex1
throw ex2
dmd 1.061 on linux:
throw ex1
throw ex2
throw ex3
Error: ex 3
dmd 2.046 on windows:
throw ex1
throw ex2
object.Exception: ex 2
dmd 2.047 (close to svn tip) on linux:
throw ex1
throw ex2
throw ex3
object.Exception: ex 3
--- <cut the stack trace> ---
So.. still different.
Comment #5 by clugdbug — 2010-12-10T00:40:35Z
The behaviour of this bug changed in DMD2.048, (possibly as a result of the fix for bug 4339?)
Now, on Windows DMD2.050 produces:
throw ex1
throw ex2
object.Exception: ex 2
object.Exception: ex 1
Which I don't understand at all.
On D1.065, it is still:
throw ex1
throw ex2
Error: ex 2
Does it still work on D2 Linux?
Comment #6 by braddr — 2010-12-10T00:46:09Z
building against tip of trunk for dmd/druntime/phobos, the output:
throw ex1
throw ex2
throw ex3
object.Exception: ex 3
----------------
<snip stack trace>
object.Exception: ex 2
----------------
<snip stack trace>
object.Exception: ex 1
----------------
<snip stack trace>
Comment #7 by clugdbug — 2010-12-10T01:48:42Z
I think the D2 change was actually caused by druntime svn 358. Sean's comment:
Changed how exception chaining works. Now, the original exception will be retained and continue propagating even if other exceptions are thrown during stack unwinding. These exceptions will be chained via the 'next' pointer and regarded as collateral damage, as per TDPL.
Is that supposed to apply in this case? Either druntime is wrong, or (more likely) the spec needs to be updated.
Comment #8 by dfj1esp02 — 2010-12-12T09:39:15Z
I'd prefer Sean's description, though the real issue is the 3rd finally block is not executed.
Comment #9 by clugdbug — 2010-12-12T23:50:25Z
Reduced test case. Only two finally clauses are required, provided that each try{} block contains a throw statement. This shows that nesting of finally statements doesn't work at all on Windows. Raising priority to critical.
----
import std.stdio;
void main()
{
try
{
try
{
writefln("throw ex1");
throw new Exception("ex 1");
}
finally
{
writefln("throw ex2");
throw new Exception("ex 2");
}
}
finally
{
writefln("finally"); // never reached
}
}
Comment #10 by clugdbug — 2010-12-13T13:20:36Z
I'm not sure that this is a compiler bug. It may be druntime.
With this change:
----
deh.c, _d_framehandler(), line 210
+ else if (prev_ndx == -1)
+ { // Exception didn't get caught.
+ // Call all the finally blocks skipped in this frame
+ _d_local_unwind(handler_table, frame, ndx);
+ }
}
}
return ExceptionContinueSearch;
----
the finally clauses are called correctly. This isn't a correct patch,
but I think it demonstrates that the exception tables are set up reasonably correctly. I couldn't find anything wrong with them.
My limited understanding of exception handling is based on this article:
http://www.microsoft.com/msj/0197/exception/exception.aspx
Comment #11 by clugdbug — 2010-12-15T17:22:58Z
This is definitely a druntime issue (though it could be an issue with the C stdlib). The bug must lie in the druntime/src/rt/deh.c
In the test case, here's my understanding of what happens:
The first exception is thrown.
Every try block is queried (in _d_framehandler() )to see if it's handled.
Nothing in main() handles it, but something else does. (I think in this case, it's a catch-all block around main).
Then, the unwinding starts. _d_framehandler() runs this:
// Have system call all finally blocks in intervening frames
int retval = _global_unwind((ESTABLISHER_FRAME *)frame, exception_record);
_global_unwind is a wrapper around the Windows system function RtlUnwind.
That calls _dframehandler() again, with EXCEPTION_UNWIND this time.
This calls _d_local_unwind(), to run all of the finally blocks
_d_local_unwind() is also in druntime/deh.c.
In the test case, the finally block contains a throw, so the procedure is repeated. Again it doesn't get caught until the catch-all.
As before, global_unwind and then local_unwind get called.
The first thing _d_local_unwind() does is set up a double-fault exception handler.
When it begins the unwind, the double-fault handler gets triggered.
The double-fault handler basically just does this:
if (!(ExceptionRecord->ExceptionFlags & EXCEPTION_UNWIND))
return ExceptionContinueSearch;
*((ESTABLISHER_FRAME **)DispatcherContext) = EstablisherFrame;
return ExceptionCollidedUnwind;
Because it returns ExceptionCollidedUnwind, Windows is supposed to abandon the original unwinding, and the new unwinding
should take over. But that's doesn't seem to be what happens.
No further unwinding occurs. To my suprise, it returns from _global_unwind
into _d_framehandler, back in the original catch handler, and all the variables seem to be unchanged (so you have no way of detecting what happened).
Dunno where the problem is. Maybe it could be in _global_unwind in the C runtime?
This stuff is amazingly undocumented by Microsoft.
This is the most detailed info that I've found:
http://www.nynaeve.net/?p=106
See point 7.
And here is more detail about collided unwinds.
http://www.nynaeve.net/?p=107
Comment #12 by clugdbug — 2010-12-29T00:25:26Z
On D2, the behaviour is wrong on all platforms, not just Windows. Roughly speaking, Linux does exception chaining incorrectly, and Windows doesn't do it at all.
These two commits are a first step towards fixing the bug: the problematic code in the C runtime is replaced with a D implementation (the buggy behaviour isn't yet changed though).
http://www.dsource.org/projects/druntime/changeset/458http://www.dsource.org/projects/druntime/changeset/459