Custom default exception handler?

Nick Sabalausky SeeWebsiteToContactMe at semitwist.com
Tue Feb 11 19:31:32 PST 2014


On 2/11/2014 6:35 PM, Sean Kelly wrote:
> Throw a static exception (maybe even derived directly from Throwable),
> similar to OutOfMemory, with a custom toString.  That should eliminate
> the formatting you don't like and will prevent the trace from occurring
> as well (see rt/deh.d in Druntime--the trace isn't run if you throw
> typeid(t).init.ptr).

Hmm, my custom toString isn't being executed. Am I doing something wrong 
here? Same result if I inherit direct from Throwable instead of Exception.

class Fail : Exception
{
     private this()
     {
         super(null);
     }

     private static Fail opCall(string msg, string file=__FILE__, int 
line=__LINE__)
     {
         auto f = cast(Fail) cast(void*) Fail.classinfo.init;

         f.msg  = msg;
         f.file = file;
         f.line = line;

         return f;
     }

     override string toString()
     {
     writeln("In Fail.toString()");
         return "someapp: ERROR: "~msg;
     }

}

void fail(string msg, string file=__FILE__, int line=__LINE__)
{
     throw Fail(msg, file, line);
}

void main()
{
     fail("Shit/fan collision detected.");
}

Output:
scriptlike.Fail at scriptlike.d(106): Shit/fan collision detected.



More information about the Digitalmars-d-learn mailing list