Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created December 17, 2015 16:35
Show Gist options
  • Save krysseltillada/4e755b41cb69a522aeae to your computer and use it in GitHub Desktop.
Save krysseltillada/4e755b41cb69a522aeae to your computer and use it in GitHub Desktop.
exception handling
#include "EngineHandler.hpp"
/*
implementation file
*/
namespace EngineHandler {
MainEngineHandler::MainEngineHandler ( const std::string &errMsg, const int &errCode ) try : /// initializes a constructor with a try block and catch block handler for exception purposes
errorMsg ( errMsg ), errorCode ( errCode ) { } catch ( std::runtime_error &e ) {
std::cerr << e.what () << std::endl;
throw std::runtime_error (e);
}
MainEngineHandler::MainEngineHandler (const std::string &errMsg) try :
errorMsg ( errMsg ) { } catch ( std::runtime_error &e ) {
std::cerr << e.what () << std::endl;
throw std::runtime_error (e);
}
MainEngineHandler::MainEngineHandler (const int &errCode) try :
errorCode ( errorCode ) { } catch ( std::runtime_error &e ) {
std::cerr << e.what () << std::endl;
throw std::runtime_error (e);
}
}
namespace EngineSubHandler {
SoundError::SoundError (const std::string &errMsg, const int &errCode) :
MainEngineHandler (errMsg, errCode) { }
std::string SoundError::errorMessage () { return this->errorMsg; }
unsigned int SoundError::GeterrorCode () { return this->errorCode; }
GraphicsError::GraphicsError ( const std::string &errMsg, const int &errCode ) :
MainEngineHandler (errMsg, errCode) { }
std::string GraphicsError::errorMessage () { return this->errorMsg; }
unsigned int GraphicsError::GeterrorCode () { return this->errorCode; }
ResourceError::ResourceError ( const std::string &errMsg, const int &errCode ) :
MainEngineHandler (errMsg, errCode) { }
std::string ResourceError::errorMessage () { return this->errorMsg; }
unsigned int ResourceError::GeterrorCode () { return this->errorCode; }
SystemError::SystemError ( const std::string &errMsg, const int &errCode ) :
MainEngineHandler (errMsg, errCode) { }
std::string SystemError::errorMessage () { return this->errorMsg; }
unsigned int SystemError::GeterrorCode () { return this->errorCode; }
MemoryError::MemoryError ( const std::string &errMsg, const int &errCode ) :
MainEngineHandler (errMsg, errCode) { }
std::string MemoryError::errorMessage () { return this->errorMsg; }
unsigned int MemoryError::GeterrorCode () { return this->errorCode; }
}
#ifndef ENGINE_HANDLER_HPP
#define ENGINE_HANDLER_HPP
#include <stdexcept> /// std::runtime_error
#include <iostream> /// std::cerr
/*
header file
*/
namespace EngineHandler {
namespace ErrorCode { /// state of the following error
enum ECode {
Sound = 202,
Graphics = 203,
Resource = 204,
System = 205,
Memory = 206
};
}
class MainEngineHandler {
public:
MainEngineHandler () = default; /// basically synthesize's a default constructor
explicit MainEngineHandler (const std::string &, const int &); /// constructor with params string and int
explicit MainEngineHandler (const std::string &); /// constructor with param string
explicit MainEngineHandler (const int &); /// constructor with param int
virtual ~MainEngineHandler () = default; /// basically synthesize's a default destructor for the wrapped class
protected:
std::string errorMsg; /// stores information about the error
unsigned int errorCode; /// state of the error
};
}
namespace EngineSubHandler {
class SoundError : public EngineHandler::MainEngineHandler { /// inherits all the MainEngineHandler subobjects
public:
SoundError (const std::string &, const int &); /// constructs a particular error object and initialize's the base part
std::string errorMessage (); /// gets the error message
unsigned int GeterrorCode (); /// gets the error state
};
class GraphicsError : public EngineHandler::MainEngineHandler {
public:
GraphicsError (const std::string &, const int &);
std::string errorMessage ();
unsigned int GeterrorCode ();
};
class ResourceError : public EngineHandler::MainEngineHandler {
public:
ResourceError (const std::string &, const int &);
std::string errorMessage ();
unsigned int GeterrorCode ();
};
class SystemError : public EngineHandler::MainEngineHandler {
public:
SystemError (const std::string &, const int &);
std::string errorMessage ();
unsigned int GeterrorCode ();
};
class MemoryError : public EngineHandler::MainEngineHandler {
public:
MemoryError (const std::string &, const int &);
std::string errorMessage ();
unsigned int GeterrorCode ();
};
}
#endif // ENGINE_HANDLER_HPP
#include <iostream>
#include "EngineHandler.hpp"
int main ()
{
EngineSubHandler::GraphicsError err("error loading shaders",
EngineHandler::ErrorCode::Graphics);
std::cout << err.GeterrorCode () << std::endl
<< err.errorMessage () << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment