Collateral exceptions seem to be broken

Andrej Mitrovic andrej.mitrovich at gmail.com
Sun May 8 13:31:30 PDT 2011


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:

object.Exception at test.d(26): thrown from foo
object.Exception at test.d(38): thrown from bar #3
object.Exception at test.d(38): thrown from bar #2
object.Exception at test.d(38): thrown from bar #1
object.Exception at test.d(38): thrown from bar #3
object.Exception at test.d(38): thrown from bar #2
object.Exception at test.d(38): thrown from bar #1
object.Exception at test.d(38): thrown from bar #2
object.Exception at test.d(38): thrown from bar #1
object.Exception at test.d(38): thrown from bar #1

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

writes:
object.Exception at test.d(27): thrown from foo
object.Exception at test.d(39): thrown from bar #3
object.Exception at test.d(39): thrown from bar #2
object.Exception at test.d(39): thrown from bar #1

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


More information about the Digitalmars-d mailing list