exception messages

Andrej Mitrovic andrej.mitrovich at gmail.com
Tue Oct 23 18:03:02 PDT 2012


On 10/24/12, Greg <greg.steffensen at gmail.com> wrote:
> I'm attempting to learn D through a personal project, and can't
> figure out how to get the message from an exception.

I don't know why Throwable is not documented
(http://dlang.org/phobos/object.html#Throwable), but you can use the
.msg field:

import std.exception;
import std.stdio;

void main()
{
    try {
        enforce(0, "message");
    }
    catch (Exception ex)
    {
        writeln(ex.msg);
    }
}

See the DMD2/src/druntime/src/object_.d file for the class definition
of Throwable and Exception, Throwable has these fields:

    string      msg;
    string      file;
    size_t      line;
    TraceInfo   info;
    Throwable   next;


More information about the Digitalmars-d-learn mailing list