'idiomatic' porting of c and or c++ code that does NULL checking

Vladimir Panteleev via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 24 19:35:35 PDT 2014


On Monday, 25 August 2014 at 02:17:47 UTC, Mike Parker wrote:
> // Put this somewhere you can import it into any module calling

Small modification for even terser error handling:

T sdlEnforce(T)(T result, string message = null)
{
   if (!result)
     throw new SdlException("SDL error: "
       ~ (message ? message ~ ": " : "")
       ~ to!string(SDL_GetError()));
   return result;
}

Usage:

gWindow = SDL_CreateWindow(...).sdlEnforce("SDL_CreateWindow");

> class SDLError : Error {

This is misuse of the Error class. Don't do this, subclass 
Exception instead. D errors should be used to indicate logic 
errors (conditions that can only arise from a bug in the 
program), not failures in 3rd-party libraries.


More information about the Digitalmars-d-learn mailing list