Project

General

Profile

Actions

Feature #4834

closed

NDN_THROW: improved replacement for BOOST_THROW_EXCEPTION

Added by Junxiao Shi about 5 years ago. Updated about 5 years ago.

Status:
Closed
Priority:
Normal
Category:
Utils
Target version:
Start date:
Due date:
% Done:

100%

Estimated time:

Description

BOOST_THROW_EXCEPTION is quite inflexible for our use cases. Since our exception types do not derive from boost::exception, in order to augment the thrown exception with additional error_info structs, each user has to wrap the constructed exception in a boost::enable_error_info call.

I'm proposing NDN_THROW as an improved alternative and drop-in replacement for BOOST_THROW_EXCEPTION. Some of the improvements over its boost counterpart are:

  • Easy chaining of error_info structs to transport arbitrary types to the catch site, e.g.:
    NDN_THROW(std::runtime_error("foo")) << my_error_info("bar") << more_info(42);
  • On platforms with boost >= 1.65, automatically embeds a stack trace up to the throw site, which can be inspected or printed at the catch site.
  • Additional _ERRNO and _NESTED variants, that automatically capture the value of errno and the active exception (to be nested in the new exception), respectively. These two macros substantially reduce repeated boilerplate code when dealing with C library functions and nested exceptions.
  • Fully compatible with boost::diagnostic_information and boost::current_exception.
  • Much shorter macro name :)
Actions #1

Updated by Junxiao Shi about 5 years ago

@Davide, please update description to explain how proposed NDN_THROW differs from BOOST_THROW_EXCEPTION.

Actions #2

Updated by Davide Pesavento about 5 years ago

  • Subject changed from NDN_THROW to NDN_THROW: improved replacement for BOOST_THROW_EXCEPTION
  • Description updated (diff)
  • Status changed from New to In Progress
Actions #4

Updated by Junxiao Shi about 5 years ago

Is nfd::getExtendedErrorMessage still necessary to print the error? If so, it should be included in ndn-cxx/util/exception.hpp.

Actions #5

Updated by Davide Pesavento about 5 years ago

getExtendedErrorMessage is not (and has never been) necessary. You can always catch the exception as a std::exception& and print the .what() message. That'll still work as usual. Or catch as boost::exception& and use get_error_info() to extract the details.

That being said, I recommend to get rid of getExtendedErrorMessage and use boost::diagnostic_information instead. It's much more powerful and supports printing all error details out of the box, including nested exceptions. If special/prettier formatting for a custom error_info is desired, it is sufficient to define a to_string() function for the corresponding specialization. This design also has the advantage of not requiring to change all catch sites to explicitly support each new error_info type.

Actions #6

Updated by Davide Pesavento about 5 years ago

  • % Done changed from 50 to 70
Actions #7

Updated by Davide Pesavento about 5 years ago

  • % Done changed from 70 to 90
Actions #8

Updated by Davide Pesavento about 5 years ago

  • Status changed from In Progress to Code review
  • % Done changed from 90 to 100
Actions #9

Updated by Davide Pesavento about 5 years ago

  • Status changed from Code review to Closed
Actions

Also available in: Atom PDF