今天一口气写了9个异常类

很简单,用宏定义

#ifndef _EXCEPTION_H 
#define _EXCEPTION_H 1 

#include <stdexcept> 
#include <iostream> 
#include <string> 

#define THROW(msg) do{ \ 
except err(__FILE__,__LINE__,__func__,msg); \ 
throw err; \ 
}while(0); 

#define THROW_1(msg,TYPE) do{ \ 
TYPE err(__FILE__,__LINE__,__func__,msg); \ 
throw err; \ 
}while(0); 

class except: public std::exception{ 
private: 
except() throw(); 
std::string errorMsg; 

std::string m_file; 
long m_line; 
std::string m_func; 

public: 
/*! \brief 输出详细的出错信息 */ 
friend std::ostream& operator<<(std::ostream& os,const except& ex){ 
std::ios::iostate state = std::ios::goodbit; 
/* const */ std::ostream::sentry ok(os); 
if ( ok ) { 
try { 
os<<"文件:"<<ex.m_file<<" 行:"<<ex.m_line<<" 函数:"<<ex.m_func 
<<" 错误信息:"<<ex.errorMsg; 
} 
catch ( ... ){ 
try 
{os.setstate( std::ios::badbit ); } 
catch (...) 
{} 
if ( ( os.exceptions( ) & os.badbit ) != 0 ) 
throw; } 
} 
os.width( 0 ); // Except for operator<<(Elem) 
os.setstate( state ); 
return os; 
} 

/*! 构造函数 */ 
except(std::string file,long line, 
std::string func,const char* msg) throw():errorMsg(msg), m_file(file), 
m_line(line),m_func(func) { 
} 
/*! 构造函数 */ 
except(std::string file,long line, 
std::string func,std::string msg) throw():errorMsg(msg), m_file(file), 
m_line(line),m_func(func) { 
} 

virtual const char* what(){ 
return errorMsg.c_str(); 
} 

/*! 获得详细的出错信息 
* 如果定义了NDEBUG,那么这个函数等同于 
* what() 
*/ 

/* 
std::string debug(){ 
std::ostringstream oss; 
oss<<"文件:"<<m_file<<" 行:"<<m_line<<" 函数:"<<m_func 
<<" 错误信息:"<<errorMsg; 
return oss.str(); 
} */ 
/*! 获得抛出异常的文件名 */ 
std::string getFileName(){return m_file;} 
/*! 获得抛出异常的函数名 */ 
std::string getFuncName(){return m_func;} 
/*! 获得抛出异常的行号 */ 
long getLineNumber(){return m_line;}; 

virtual ~except() throw(){}; 
}; 

#define NEW_EXCEPTION_TYPE_BEGIN(X,BASE) \ 
class X:public BASE{ \ 
public: \ 
X(std::string file,long line,\ 
std::string func,std::string msg) throw() :BASE(file,line,func,msg){} \ 
X(std::string file,long line, \ 
std::string func,const char* msg) throw() :BASE(file,line,func,msg){} 

#define NEW_EXCEPTION_TYPE_END \ 
}; 

/*! \brief 定义了4种逻辑错误 
* \li \c domain_error_except 逻辑错误,在程序运行前可以检测出来 
* \li invalid_argument_except 指出函数的一个无效参数 
* \li length_error_except 指出有一个超过类型size_t的最大可表现 
* 值长度的对象的企图 
* \li out_of_range_except 参数越界 
* 和三种运行期错误 
* \li overflow_except 报告一个算术上溢 
* \li range_error_except 违反后置条件 
* \li underflow_except 报告下溢 
*/ 

NEW_EXCEPTION_TYPE_BEGIN( logic_error_except,except ) 

NEW_EXCEPTION_TYPE_END 

NEW_EXCEPTION_TYPE_BEGIN( runtime_error_except,except ) 

NEW_EXCEPTION_TYPE_END 

NEW_EXCEPTION_TYPE_BEGIN( domain_error_except,logic_error_except ) 

NEW_EXCEPTION_TYPE_END 

NEW_EXCEPTION_TYPE_BEGIN( invalid_argument_except,logic_error_except ) 

NEW_EXCEPTION_TYPE_END 
NEW_EXCEPTION_TYPE_BEGIN( length_error_except,logic_error_except ) 

NEW_EXCEPTION_TYPE_END 
NEW_EXCEPTION_TYPE_BEGIN( out_of_range_except,logic_error_except ) 

NEW_EXCEPTION_TYPE_END 
NEW_EXCEPTION_TYPE_BEGIN( overflow_except,runtime_error_except ) 

NEW_EXCEPTION_TYPE_END 
NEW_EXCEPTION_TYPE_BEGIN( range_error_except,runtime_error_except ) 

NEW_EXCEPTION_TYPE_END 
NEW_EXCEPTION_TYPE_BEGIN( underflow_except,runtime_error_except ) 

NEW_EXCEPTION_TYPE_END 

#endif /* _EXCEPTION_H */

此博客中的热门博文

少写代码,多读别人写的代码

在windows下使用llvm+clang

tensorflow distributed runtime初窥