boost::exception 异常 P151
#include <boost/exception/all.hpp>
using namespace boost;
代码
#include <string> #include <boost/exception/all.hpp> using namespace boost; struct my_exception : //自定义异常类 virtual std::exception, //虚继承 virtual boost::exception //虚继承 {}; //空实现,不需要实现代码 //异常信息的类型 typedef boost::error_info<struct tag_err_no, int> err_no; typedef boost::error_info<struct tag_err_str, std::string> err_str; int main() try { try { throw my_exception() << err_no( 10 ); } catch (my_exception& e) { //获得异常内存存储的信息 cout << *get_error_info<err_no>( e ) << endl; cout << e.what() << endl; e << err_str( "other info" ); //向异常追加信息 throw; //再次抛出异常 } system( "PAUSE" ); return 0; } catch( my_exception& e ) { cout << *get_error_info<err_str>( e ) << endl; }
boost预定义异常信息类型 P156
辅助工具宏 DEFINE_ERROR_INFO
#define DEFINE_ERROR_INFO(type, name) \ typedef boost::error_info<struct tag_##name, type> name //声明一个变量 DEFINE_ERROR_INFP(int, err_no); //相当于typedef boost::error_info<struct tag_err_no, int> err_no;
获得更多的调试信息 P159
diagnostic_information(). //cout<