二、运行时的错误
一个可以进行安全检查的宏
scpp_assert.h:
[cpp]
#ifndef __SCCP_ASSERT_H__
#define __SCCP_ASSERT_H__
#include "sstream"
#ifdef SCPP_THROW_EXCEPTION_ON_BUG
#include "exception"
namespace scpp
{
class ScppAssertFailedException : public std::exception
{
public:
ScppAssertFailedException(const char *file_name, unsigned line_number, const char* message);
~ScppAssertFailedException() throw(){};
public:
virtual const char * what() const throw()
{
return what_.c_str();
}
private:
std::string what_;
};
}// namespace scpp
#endif
void SCPP_AssertErrorHandler(const char *file_name, unsigned line_number, const char *message);
#define SCPP_ASSERT(condition, msg) \
if (!(condition)) \
{ \
std::ostringstream s; \
s 《 msg; \
SCPP_AssertErrorHandler( \
__FILE__, __LINE__, s.str()。c_str()); \
}
#ifndef _DEBUG
#define SCPP_TEST_ASSERT_ON
#endif // !_DEBUG
#ifdef SCPP_TEST_ASSERT_ON
#define SCPP_TEST_ASSERT_ON(condition, msg) SCPP_ASSERT(condition, msg)
#else
#define SCPP_TEST_ASSERT_ON(condition, msg) // do nothing
#endif // !SCPP_TEST_ASSERT_ON
#endif // !_SCCP_ASSERT_H_
scpp_assert.cpp:
[cpp]
#include "stdafx.h"
#include "scpp_assert.h"
#include "iostream"
#include "stdlib.h"
#ifdef SCPP_THROW_EXCEPTION_ON_BUG
namespace scpp
{
ScppAssertFailedException::ScppAssertFailedException(const char *file_name, unsigned line_number, const char* message)