Skip to content

Commit

Permalink
add exception class and macro
Browse files Browse the repository at this point in the history
  • Loading branch information
yumetodo committed Mar 22, 2016
1 parent 3c18048 commit 4bc9c6b
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
13 changes: 13 additions & 0 deletions dxlibex/exception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*=============================================================================
Copyright (C) 2015-2016 DxLibEx project
https://github.com/Nagarei/DxLibEx/
Distributed under the Boost Software License, Version 1.0.
(See http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#ifndef DXLE_INC_EXCEPTION_HPP_
#define DXLE_INC_EXCEPTION_HPP_
#include "dxlibex/exception/runtime_error.hpp"
#include "dxlibex/exception/invalid_argument.hpp"
#include "dxlibex/exception/sound_exception.hpp"
#endif //DXLE_INC_EXCEPTION_HPP_
48 changes: 48 additions & 0 deletions dxlibex/exception/invalid_argument.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*=============================================================================
Copyright (C) 2015-2016 DxLibEx project
https://github.com/Nagarei/DxLibEx/
Distributed under the Boost Software License, Version 1.0.
(See http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#ifndef DXLE_INC_EXCEPTION_INVAID_ARGUMENT_HPP_
#define DXLE_INC_EXCEPTION_INVAID_ARGUMENT_HPP_
#include <stdexcept>
#include <string>
#include <cstdint>
#define DXLE_INVAID_ARGUMENT_THROW_WITH_MESSAGE( MESSAGE ) throw dxle::invalid_argument(__FILE__, __FUNCTION__, __LINE__, MESSAGE)
#define DXLE_INVAID_ARGUMENT_THROW_WITH_MESSAGE_IF( EXPR, MESSAGE ) if( EXPR ) throw dxle::invalid_argument(__FILE__, __FUNCTION__, __LINE__, #EXPR, MESSAGE)
namespace dxle {
namespace exception{
using namespace std::literals;
class invalid_argument : public std::invalid_argument {
protected:
invalid_argument(const char* except_name, const char* sorce_name, const char* func_name, std::uint64_t line, const char* expression, const std::string& msg)
: std::invalid_argument(
"exception : "s + except_name + '\n'
+ " in " + sorce_name + "\n"
+ " " + func_name + "() (line." + std::to_string(line) + ")\n"
+ " follow by below\n"
+ " " + expression
+ ((msg.empty() || msg[0] == '\0') ? "\n" : "\n MESSAGE : " + msg + "\n")
)
{}
invalid_argument(const char* except_name, const char* sorce_name, const char* func_name, std::uint64_t line, const std::string& msg)
: std::invalid_argument(
"exception : "s + except_name + '\n'
+ " in " + sorce_name + "\n"
+ " " + func_name + "() (line." + std::to_string(line) + ")\n"
+ ((msg.empty() || msg[0] == '\0') ? "\n" : " MESSAGE : " + msg + "\n")
)
{}
public:
invalid_argument(const char* sorce_name, const char* func_name, std::uint64_t line, const char* expression, const std::string& msg)
: invalid_argument("invalid_argument", sorce_name, func_name, line, expression, msg) {}
invalid_argument(const char* sorce_name, const char* func_name, std::uint64_t line, const std::string& msg)
: invalid_argument("invalid_argument", sorce_name, func_name, line, msg) {}
};

}
using exception::invalid_argument;
}
#endif //DXLE_INC_EXCEPTION_INVAID_ARGUMENT_HPP_
48 changes: 48 additions & 0 deletions dxlibex/exception/runtime_error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*=============================================================================
Copyright (C) 2015-2016 DxLibEx project
https://github.com/Nagarei/DxLibEx/
Distributed under the Boost Software License, Version 1.0.
(See http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#ifndef DXLE_INC_EXCEPTION_RUNTIME_ERROR_HPP_
#define DXLE_INC_EXCEPTION_RUNTIME_ERROR_HPP_
#include <stdexcept>
#include <string>
#include <cstdint>
#define DXLE_RUNTIME_ERROR_THROW_WITH_MESSAGE( MESSAGE ) throw dxle::runtime_error(__FILE__, __FUNCTION__, __LINE__, MESSAGE)
#define DXLE_RUNTIME_ERROR_THROW_WITH_MESSAGE_IF( EXPR, MESSAGE ) if( EXPR ) throw dxle::runtime_error(__FILE__, __FUNCTION__, __LINE__, #EXPR, MESSAGE)
namespace dxle {
namespace exception{
using namespace std::literals;
class runtime_error : public std::runtime_error {
protected:
runtime_error(const char* except_name, const char* sorce_name, const char* func_name, std::uint64_t line, const char* expression, const std::string& msg)
: std::runtime_error(
"exception : "s + except_name + '\n'
+ " in " + sorce_name + "\n"
+ " " + func_name + "() (line." + std::to_string(line) + ")\n"
+ " follow by below\n"
+ " " + expression
+ ((msg.empty() || msg[0] == '\0') ? "\n" : "\n MESSAGE : " + msg + "\n")
)
{}
runtime_error(const char* except_name, const char* sorce_name, const char* func_name, std::uint64_t line, const std::string& msg)
: std::runtime_error(
"exception : "s + except_name + '\n'
+ " in " + sorce_name + "\n"
+ " " + func_name + "() (line." + std::to_string(line) + ")\n"
+ ((msg.empty() || msg[0] == '\0') ? "\n" : " MESSAGE : " + msg + "\n")
)
{}
public:
runtime_error(const char* sorce_name, const char* func_name, std::uint64_t line, const char* expression, const std::string& msg)
: runtime_error("runtime_error", sorce_name, func_name, line, expression, msg) {}
runtime_error(const char* sorce_name, const char* func_name, std::uint64_t line, const std::string& msg)
: runtime_error("runtime_error", sorce_name, func_name, line, msg) {}
};

}
using exception::runtime_error;
}
#endif //DXLE_INC_EXCEPTION_RUNTIME_ERROR_HPP_
28 changes: 28 additions & 0 deletions dxlibex/exception/sound_exception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*=============================================================================
Copyright (C) 2015-2016 DxLibEx project
https://github.com/Nagarei/DxLibEx/
Distributed under the Boost Software License, Version 1.0.
(See http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#ifndef DXLE_INC_EXCEPTION_SOUND_EXCEPTION_HPP_
#define DXLE_INC_EXCEPTION_SOUND_EXCEPTION_HPP_
#include "dxlibex/exception/runtime_error.hpp"
#define DXLE_SOUND_ERROR_THROW_WITH_MESSAGE( MESSAGE ) throw dxle::sound_error(__FILE__, __FUNCTION__, __LINE__, MESSAGE)
#define DXLE_SOUND_ERROR_THROW_WITH_MESSAGE_IF( EXPR, MESSAGE ) if( EXPR ) throw dxle::sound_error(__FILE__, __FUNCTION__, __LINE__, #EXPR, MESSAGE)

namespace dxle {
namespace exception{
class sound_error : public dxle::exception::runtime_error {
public:
sound_error(const char* sorce_name, const char* func_name, std::uint64_t line, const char* expression, const std::string& msg)
: dxle::exception::runtime_error("sound_error", sorce_name, func_name, line, expression, msg) {}
sound_error(const char* sorce_name, const char* func_name, std::uint64_t line, const std::string& msg)
: dxle::exception::runtime_error("sound_error", sorce_name, func_name, line, msg) {}
};

}
using exception::sound_error;
}

#endif //DXLE_INC_EXCEPTION_SOUND_EXCEPTION_HPP_

0 comments on commit 4bc9c6b

Please sign in to comment.