Collateral Exceptions, Throwable vs Exception

Andrej Mitrovic andrej.mitrovich at gmail.com
Thu Aug 19 12:43:59 PDT 2010


TDPL code that should work:

class MyException : Exception
{
    this(string s)
    {
        super(s);
    }
}

void fun()
{
    try
    {
        throw new Exception("thrown from fun");
    }
    finally
    {
        gun(100);
    }
}

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

import std.stdio, std.conv;

unittest
{
    try
    {
        fun();
    }
    catch (Exception e)
    {
        writeln("Primary exception: ", typeid(e), " ", e);
        while ((e = e.next) !is null)
        {
            writeln("Collateral exception: ", typeid(e), " ", e);
        }
    }
}

void main()
{
}

Errors out:
(49): Error: cannot implicitly convert expression (e.next) of type object.Throwable to object.Exception

Why would the return type of e.next be Throwable if e is an Exception type?

I've tried casting e.next to type Exception, but that creates an entire mess.


More information about the Digitalmars-d mailing list