[Turn] Exception handling in boost library

When you are faced with tens of millions of lines of projects, when you see that the system outputs abnormal information, have you ever thought about it, if it can output file names, line numbers and other information, how good? Brain juice.

      Using the boost library today will easily solve this problem.

1. Basic usage of boost exception

First look at the general practice of using exception classes in STL:

 
// 使用STL定义自己的异常 
class MyException : public std::exception
{
public:
    MyException(const char * const &msg):exception(msg)
    {
    }
    MyException(const char * const & msg, int errCode):exception(msg, errCode)
    {
    }
};
 
void TestException()
{
    try
    {
        throw MyException("error");
    }
    catch(std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }
}

The implementation scheme of boost library is:

 
//使用Boost定义自己的异常 
#include <boost/exception/all.hpp>
class MyException : virtual public std::exception,virtual public 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;
void TestException()
{
     try
     {
         throw MyException() << err_no(10) << err_str("error");
     }
     catch(std::exception& e)
     {
         std::cout << *boost::get_error_info<err_str>(e) << std::endl;
     }
}

 

The boost library separates the exception class and the error message, making the error message more flexible, where typedef boost::error_info<struct tag_err_no, int> err_no;

Define an error message class, tag_err_no has no practical meaning and is only used for identification, in order to allow the same type to instantiate multiple error message classes.

 

2. Use boost::enable_error_info to convert standard exception classes to boost exception classes

 
class MyException : public std::exception{}; 
 
#include <boost/exception/all.hpp>
typedef boost::error_info<struct tag_err_no, int> err_no;
typedef boost::error_info<struct tag_err_str, std::string> err_str;
 
void TestException()
{
    try
    {
        throw boost::enable_error_info(MyException()) << err_no(10) << err_str("error");
    }
    catch(std::exception& e)
    {
        std::cout << *boost::get_error_info<err_str>(e) << std::endl;
    }
}

With the exception class of boost, when an exception is thrown, more information can be inserted, such as function name, file name, and line number.

3. Use BOOST_THROW_EXCEPTION to let the standard exception class provide more information

 
// 使用STL定义自己的异常 
class MyException : public std::exception
{
public:
    MyException(const char * const &msg):exception(msg)
    {
    }
    MyException(const char * const & msg, int errCode):exception(msg, errCode)
    {
    }
};
#include <boost/exception/all.hpp>
void TestException()
{
    try
    {
        //让标准异常支持更多的异常信息
        BOOST_THROW_EXCEPTION(MyException("error"));
    }
    catch(std::exception& e)
    {
        //使用diagnostic_information提取所有信息
        std::cout << boost::diagnostic_information(e) << std::endl;
    }
}

 

image

We hardly need to modify the previous exception class to allow it to provide more exception information.

Guess you like

Origin blog.csdn.net/gouguofei/article/details/104613996