libSDL2pp  0.16.0
C++11 bindings/wrapper for SDL2
Public Member Functions | List of all members
SDL2pp::Exception Class Reference

Exception object representing SDL2 error More...

#include <SDL2pp/Exception.hh>

Inheritance diagram for SDL2pp::Exception:

Public Member Functions

 Exception (const char *function)
 Construct exception, storing result of SDL_GetError() More...
 
 Exception (const Exception &)=default
 Copy constructor.
 
virtual ~Exception () noexcept
 Destructor.
 
std::string GetSDLFunction () const
 Get name of SDL function which caused an error. More...
 
std::string GetSDLError () const
 Get SDL2 error text. More...
 

Detailed Description

Exception object representing SDL2 error

Internally, libSDL2pp checks return value of each SDL2 function it calls which may fail. If the function fails, SDL2pp::Exception is thrown, and SDL2 error which explains cause of function failure is stored in the exception and may be extracted later.

what() usually contains a name of SDL2 function which failed, e.g. "SDL_Init() failed"

Note: this Exception object is used to report errors from SDL2 satellite libraries (SDL_image, SDL_mixer, SDL_ttf) as well. Though they use their own error handling functions (IMG_GetError, Mix_GetError, TTF_GetError), those are (currently) just macros pointing to SDL_GetError. We currently rely on that. If that changes, we'll need a hierarchy of specific exceptions.

Usage example:

{
try {
SDL2pp::SDL sdl(SDL_INIT_VIDEO);
} catch (SDL2pp::Exception& e) {
std::cerr << "Fatal error:"
<< e.what() // "SDL_Init failed"
<< ", SDL error: "
<< e.GetSDLError() // "x11 not available"
<< std::endl;
}
}

Constructor & Destructor Documentation

◆ Exception()

SDL2pp::Exception::Exception ( const char *  function)
explicit

Construct exception, storing result of SDL_GetError()

Parameters
[in]functionName of SDL function which generated an error

Member Function Documentation

◆ GetSDLError()

std::string SDL2pp::Exception::GetSDLError ( ) const

Get SDL2 error text.

Returns
Stored result of SDL_GetError()
See also
http://wiki.libsdl.org/SDL_GetError

◆ GetSDLFunction()

std::string SDL2pp::Exception::GetSDLFunction ( ) const

Get name of SDL function which caused an error.

Returns
Name of function which caused an error

The documentation for this class was generated from the following files: