posted by REDFORCE 2017. 4. 7. 10:13

#1. Engine System

 + WinMain - EngineSystem

 + EngineCore

 + EngineError (현재 글)

 + MainNode


#2. FrameWork

 + Grahpics

 + Image - SpriteData

 + Layer

 + Input

 + Game

 + Manager

 + Game Interface

 + Utility


#3. Testing Module

 + 2D Image Test

 + 2D Animation Test

 + Game UI Test


-----------------------------------------------------------------------


이번 글에서는 EngineError.h 에 대해서 설명하도록 하겠습니다.


흔히 어떤 에러에 대한 처리를 위해서 try - catch 문을 이용하여


exception 처리를 하는데요.



저는 그 처리 중에 내가 알수 있을 법한 처리로 에러를 보고자


EngineError.h 를 만들어두었습니다.


먼저 코드를 보는게 이해가 빠른 분들이 계실 수도 있으니 전체 코드부터 올려드리겠습니다.

#ifndef _ENGINERROR_H
#define _ENGINERROR_H
#define WIN32_LEAN_AND_MEAN
#include <string>
#include <exception>
namespace engineErrorNS
{
const int ENGINE_FATAL_ERROR = 400;
const int ENGINE_INTERFACE_ERROR = 401;
const int ENGINE_CORE_ERROR = 403;
const int ENGINE_SYSTEM_MENU_ERROR = 404;
}
class EngineError : public std::exception
{
private:
int errorCode;
std::string message;
public:
// default constructor
EngineError() throw() :errorCode(engineErrorNS::ENGINE_FATAL_ERROR), message("Undefined Error in EngineSystem.") {}
// copy constructor
EngineError(const EngineError& e) throw() : std::exception(e), errorCode(e.errorCode), message(e.message) {}
// constructor with args
EngineError(int code, const std::string &s) throw() :errorCode(code), message(s) {}
// assignment operator
EngineError& operator= (const EngineError& rhs) throw()
{
std::exception::operator=(rhs);
this->errorCode = rhs.errorCode;
this->message = rhs.message;
}
// destructor
virtual ~EngineError() throw() {};
// override what from base class
virtual const char* what() const throw() { return this->getMessage(); }
const char* getMessage() const throw() { return message.c_str(); }
int getErrorCode() const throw() { return errorCode; }
};
#endif // !_ENGINEERROR_H
view raw engineError.h hosted with ❤ by GitHub



위와 같이 먼저 


1. #include <exception>을 이용합니다.

2. 그리고 간단히 내가 정의할 에러 코드를 namespace로 묶어두었습니다.

3. EngineError Class는 std::exception을 부모로 받습니다.

4. 생성자에 throw( ) 를 이용하여 에러 코드와 메세지를 던지도록 하게합니다.

5. 간단히 operator= 에도 메세지를 받을 수 있도록 재정의 해줍니다.



이게 다 입니다.


사용 법은??


이미 우리는 engineSystem.cpp 에서 EngineError를 사용하는 것을 보셨을 겁니다.


그곳(engineError.cpp)을 참조하시는게 빠르니 


직접 해보시거나 이런식으로 만들어보면서 쓰시면 좋을 것 같습니다.

'MyProject > SephyEngine' 카테고리의 다른 글

June_Engine #2_Graphics.h  (0) 2017.04.07
June_Engine #1_MainNode  (0) 2017.04.07
June_Engine #1_EngineCore.cpp (2)  (0) 2017.04.07
June_Engine #1_EngineCore.h (1)  (0) 2017.04.06
June_Engine #1_EngineSystem  (0) 2017.04.06