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

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 24 20:19:08 PDT 2014


On 8/25/2014 11:35 AM, Vladimir Panteleev wrote:
> 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.

I use Exception for recoverable errors and Error for those that aren't.

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com



More information about the Digitalmars-d-learn mailing list