[exception] Conditional error_info addition
Is there a way to add error_info to an exception object conditionally? In my use case I throw upon error during Windows Registry manipulation. One of the data attached to the exception object is the path to the registry key on which operation failed. The problem however is that sometimes (while the library is still developed...) the failure is due to internal code error: the handle to the key is no longer valid because it was closed (erroneously) previously. In such case obtaining path to the key will yield an empty string. I don't want to attach an empty string. I would prefer not to attach anything. (Now actually in this particular case it might be even useful to attach an empty string which is then a clear indication that the key handle was invalid. However I hope that my point is clear here.) Currently I would have to do something like: ExceptionType e; std::wstring path = getPath( hKey ); if ( !path.empty() ) e << PathInfo( path ); BOOST_THROW_EXCEPTION( e ); This code however involves more copying with little hopes for optimization. And it is obviously long. If this was to be done for two or three data items the throwing code would become quite long... Can anything be done about it? Can we have some form of conditional error_info insertion? Adam Badura
On Mon, Feb 15, 2010 at 11:22 PM, Adam Badura
In my use case I throw upon error during Windows Registry manipulation. One of the data attached to the exception object is the path to the registry key on which operation failed. The problem however is that sometimes (while the library is still developed...) the failure is due to internal code error: the handle to the key is no longer valid because it was closed (erroneously) previously.
Is this situation a bug? If that's the case I'd do something like: PathInfo assert_path_info( std::string const & s ) { assert(!s.empty()); return PathInfo(s); } .... BOOST_THROW_EXCEPTION(ExceptionType() << assert_path_info(getPath(hKey)));
Can anything be done about it? Can we have some form of conditional error_info insertion?
template <class Info>
struct cond_info
{
typename Info::value_type value;
bool cond;
cond_info( typename Info::value_type const & value, bool cond ):
value(value), cond(cond)
{
}
};
template
On Tue, Feb 16, 2010 at 1:54 AM, Adam Badura
Is this situation a bug? If that's the case I'd do something like:
No. Its not.
Like that?
Yes. This would do. Will anything like this make it into Boost or you don't think it is useful enough?
It's a possibility, but I'm not sure I understand the use case well enough. In the use case you're describing, I would simply make getPath throw if it can't produce a valid string for whatever reason. I do use optional error info in exceptions but not like this. Example would be boost::errinfo_file_name, which may not be present in all read_error exceptions because some read errors do not involve files. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
It's a possibility, but I'm not sure I understand the use case well enough. In the use case you're describing, I would simply make getPath throw if it can't produce a valid string for whatever reason.
Firstly I don't want getKeyPath to throw because: 1) It only adds more info to true failure description (the thrown exception object), so even if it does fail its a lesser error. I just want to skip that info in such case and throw real exception. 2) It is acceptable for it to fail. If the key handle is invalid (because it was prematurely closed for example) then the function fails but it is OK. Again I want to prevent original error. Adam Badura
participants (2)
-
Adam Badura
-
Emil Dotchevski