Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8820361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:35:24+00:00 2026-06-14T05:35:24+00:00

Should my exception classes provide details in public fields in addition to what() ?

  • 0

Should my exception classes provide details in public fields in addition to what()?

Consider for example boost::property_tree::ptree_bad_path which might give a message:

"No such node (mynode.value1)"

The only way to access the path ("mynode.value1") is by parsing the string. Is there an argument against adding additional public fields to carry such information, that is:

class ptree_bad_path : public ptree_error {
public:
  const std::string path; // <- additional detail by public field
....

Are there drawbacks to this approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:35:25+00:00Added an answer on June 14, 2026 at 5:35 am

    In theory, you run the risk of having your program terminate if you ever have two unhandled exceptions at the same time. This is, however, a rather rare situation.

    • Throwing during the preparation of the exception: fine (though you won’t get the exception you expected)
    • Throwing during the copy of the exception (often elided, avoidable): crash
    • Throwing during the unwinding: crash
    • Throwing during the handling of the exception (catch): fine (after all, rethrowing a different exception is common)

    So, the avoidable risk here is if the copy constructor of your exception might happen to throw. It is trivial to elude the issue by moving the state off to a shared_ptr contained within the exception. It makes copies a bit “special” (since they share their state with the original) but if it’s documented properly it should not cause any grief.

    The greater risk is during stack unwinding. It only occurs if a destructor throws, though.

    Personally, the exceptions I use contain:

    • an error code (for the API to display/encode properly, all error messages are mapped to a code, it helps in Chinese/Korean/Japanese, really)
    • a log message, with some details (ID/name of the item that cause the issue, original error when translating another exception, whatever helps!)
    • the function/file/line at which the exception was thrown
    • the time at which the exception thrown
    • additional notes (appended “on the fly” during stack unwinding)
    • a complete backtrace (using Linux specific functions)

    The only controversial point here is the on the fly bit, since it might effectively crash. On the other hand, I work on servers so crashes are easily (and urgently) fixed and for the last 5 years I was careful enough not to cause a crash (this way ;)).

    Note that this scheme is obviously only available if you use exceptions sparsely. If you routinely throw a dozen exceptions per task, the performance might be unnacceptable. On the other hand, the Zero Cost model used by major compilers already harshly penalize the exceptional path so…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Should exception classes be part of the class which may throw them or should
I have a when in JBehave which under certain circumstances should throw an exception.
When are custom Exception classes most-valuable? Are there cases when they should or should
Should I create private static final String = Some exception message or leave it
Suppose I want to reflect exception in the log. Should I pass stack trace
I'm convinced at this point that I should be creating subclasses of std::exception for
Should we declare the private fields as volatile if the instanced are used in
Let's say we have several exception classes that extends exception. My question is, in
Suppose that I am using a library X that specifies for example that exception.BaseError
I have two classes (A and B) which depend on each other in following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.