Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to log exceptional situations in C++?
    primarykey
    data
    text
    <p>When writing a function, my implementation very frequently looks like this:</p> <ul> <li>Call a subfunction</li> <li>If this subfunction fails to execute (because of an exceptional situation): log this failure and abort the current function</li> <li>Otherwise continue calling other subfunctions, which in turn can fail</li> </ul> <p>A crucial part is the logging. Every function that fails should add a short description to the log. This way, at the level where the exception is handled, the user can be shown a detailed error message.</p> <p>For example, consider an application where a new user account can be created, and there is a problem with the database connection. The following inverse stack trace results:</p> <ul> <li>SQLDriverConnect() -> "SQLDriverConnect error: Data source not found and no default driver specified"</li> <li>OpenDatabaseConnection() -> "Failed to open database connection" </li> <li>CreateUser() -> "Failed to create the new user"</li> <li>ValidateAndSaveNewUserAccount() -> "Failed to save the user profile"</li> <li>Catch the exception and show the logged messages to the user</li> </ul> <p>Using the exceptions feature, I would implement this as follows:</p> <pre><code>void CreateUser() { try { OpenDatabaseConnection(); } catch(std::exception&amp; e) { e.AddLog("Failed to create the new user"); throw; } //... } </code></pre> <p>Using a simple return value, I'd write the following: </p> <pre><code>bool CreateUser(Log&amp; log) { if (!OpenDatabaseConnection(log)) { log.Add("Failed to create the new user"); return false; } //... return true; } </code></pre> <p>I find both implementations equally good. Therefore, I don't see much advantage in using exceptions. I am well aware that exception handling is generally considered a useful feature, but I don't really understand why. A long time ago, I used exception handling extensively, but I didn't see the big advantage, so now I never use them anymore. Hence my questions:</p> <ul> <li>Using exceptions, how can this be implemented in C++ more concisely?</li> <li>If not, what is then the advantage of throwing exceptions over returning an "is successful" boolean?</li> </ul> <p><b>Note:</b> I use the term <b>logging</b> as "collecting an explanation of what went wrong, so it can be presented to the user later on". I'd rather not store that explanation in a global collection of log messages (in memory, in a file or in a database), as it directly describes the specific exception. </p> <p><b>Update</b>: Thanks for you responses so far. I understand that exceptions are only useful if the user doesn't need detailed feedback on what went wrong. (Please correct me if I misinterpreted this.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload