Is there a danger associated with streaming data to the C ++ exception class?

Pay attention to the following code:

throw my_exception() << "The error " << error_code << " happened because of " << reasons;

The helper code will look something like this:

class my_exception
{
   public:
      template <typename T>
      my_exception & operator << (const T & value)
      {
         // put the value somewhere
         return *this;
      }
      // etc.
};

Is there any reason that would make it so throwdangerous or simply ineffective compared to the alternative below?

std::stringstream s;
s << "The error " << error_code << " happened because of " << reasons;
throw my_exception(s.str());
+4
source share
1 answer

You create an exception object through several function calls (overloads operator<<), all of which occur before the exception is thrown. This is no different from regular program execution.

, , - (, ).

, .

+4

Source: https://habr.com/ru/post/1692446/


All Articles