On May 9, 11 04:31, Andrej Mitrovic wrote:
import std.stdio;
import std.conv;
import std.exception;

void main()
{
     try
     {
         foo();
     }
     catch (Exception e)
     {
         for (Throwable t = e; t !is null; t = t.next)
         {
             writeln(t);
         }
     }
}

void foo()
{
     try
     {
         throw new Exception("thrown from foo");
     }
     finally
     {
         bar(3);
     }
}

void bar(int x)
{
     try
     {
         throw new Exception(text("thrown from bar #", x));
     }
     finally
     {
         if (x>  1)
         {
             bar(x - 1);
         }
     }
}

DMD 2.052, XP 32bit:

[email protected](26): thrown from foo
[email protected](38): thrown from bar #3
[email protected](38): thrown from bar #2
[email protected](38): thrown from bar #1
[email protected](38): thrown from bar #3
[email protected](38): thrown from bar #2
[email protected](38): thrown from bar #1
[email protected](38): thrown from bar #2
[email protected](38): thrown from bar #1
[email protected](38): thrown from bar #1

Simply calling writeln(e) will work:
void main()
{
     try
     {
         foo();
     }
     catch (Exception e)
     {
         writeln(e);
     }
}

writes:
[email protected](27): thrown from foo
[email protected](39): thrown from bar #3
[email protected](39): thrown from bar #2
[email protected](39): thrown from bar #1

However that doesn't help if you want to iterate through the
exceptions one at a time.

Not reproducible on OS X with druntime on git master. I get

[email protected](24): thrown from foo
----------------
5   y                                   0x000020b6 void y.foo() + 86
6   y                                   0x00002036 _Dmain + 14
<<rest of stack trace omitted>>
[email protected](36): thrown from bar #3
----------------
5   y                                   0x00002141 void y.bar(int) + 105
6   y                                   0x000020d0 void y.foo() + 112
7   y                                   0x00002036 _Dmain + 14
<<rest of stack trace omitted>>
[email protected](36): thrown from bar #2
----------------
5   y                                   0x00002141 void y.bar(int) + 105
6   y                                   0x00002160 void y.bar(int) + 136
7   y                                   0x000020d0 void y.foo() + 112
8   y                                   0x00002036 _Dmain + 14
<<rest of stack trace omitted>>
[email protected](36): thrown from bar #1
----------------
5   y                                   0x00002141 void y.bar(int) + 105
6   y                                   0x00002160 void y.bar(int) + 136
7   y                                   0x00002160 void y.bar(int) + 136
8   y                                   0x000020d0 void y.foo() + 112
9   y                                   0x00002036 _Dmain + 14
<<rest of stack trace omitted>>

Reply via email to