Setter in exception class

What could be the consequences of having a setter member function in an exception class? The motivation for having a setter is that sometimes there is not enough data at the throw point to properly handle the exception at the catch point; therefore, additional information must be added when unwinding the stack.

+5
source share
4 answers

Check out the Boost.Exception library, or rather, this page in the paragraph titled Adding arbitrary data to active exception objects :

void parse_file( char const * file_name )
{
    boost::shared_ptr<FILE> f = file_open(file_name,"rb");
    assert(f);
    try
    {
        char buf[1024];
        file_read( f.get(), buf, sizeof(buf) );
    }
    catch(boost::exception & e )
    {
        e << boost::errinfo_file_name(file_name);
        throw;
    }
}

Personally, I find the technique quite effective. Change the exception (adding context) and rethrow.

Java, ++ , , , , , .

+5

, , Java: , .

+3

, .

SEH ( , ++), ++. , - - .

, , "" .

- . , MSVC ( ), . , :

  • catch , . try .
  • .
+1

, , () /, .

, , . , , .

I know that some frameworks have the "Data" property, which is more or less a dictionary with arbitrary data. It sounds bad to me, although it opens up for abuse. Probably, it should not be used for making software decisions, it should be for human consumption.

If something catches the exception and makes decisions based on the X parameter, it can get pretty dirty.

+1
source

All Articles