<p>Exceptions in C++ don't need to be caught (no compile time errors) by the calling function. So it's up to developer's judgment whether to catch them using try/catch (unlike in Java).</p>

<p>Is there a way one can ensure that the exceptions thrown are always caught using try/catch by the calling function?</p>

[{'answer_id': 1109, 'author': 'C. K. Young', 'author_id': 13, 'author_profile': 'https://Stackoverflow.com/users/13', 'pm_score': 6, 'selected': True, 'text': '<p>No.</p>\n\n<p>See <a href="http://www.gotw.ca/publications/mill22.htm" rel="noreferrer">A Pragmatic Look at Exception Specifications</a> for reasons why not. </p>\n\n<p>The only way you can "help" this is to document the exceptions your function can throw, say as a comment in the header file declaring it. This is not enforced by the compiler or anything. Use code reviews for that purpose.</p>\n'}, {'answer_id': 1437, 'author': 'Ryan Fox', 'author_id': 55, 'author_profile': 'https://Stackoverflow.com/users/55', 'pm_score': -1, 'selected': False, 'text': "<p>Or you could start throwing critical exceptions. Surely, an access violation exception will <em>catch</em> your users' attention.</p>\n"}, {'answer_id': 1454, 'author': 'shsteimer', 'author_id': 292, 'author_profile': 'https://Stackoverflow.com/users/292', 'pm_score': 2, 'selected': False, 'text': '<p>Outside the scope of your question so I debated not posting this but in Java there are actually 2 types of exceptions, checked and unchecked. The basic difference is that, much like in <code>c[++]</code>, you dont have to catch an unchecked exception.</p>\n\n<p>For a good reference <a href="http://java.sun.com/docs/books/tutorial/essential/exceptions/runtime.html" rel="nofollow noreferrer">try this</a> </p>\n'}, {'answer_id': 1474, 'author': 'dlinsin', 'author_id': 198, 'author_profile': 'https://Stackoverflow.com/users/198', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>Is there a way one can ensure that the\n  exceptions thrown are always caught\n  using try/catch by the calling\n  function?</p>\n</blockquote>\n\n<p>I find it rather funny, that the Java crowd - <a href="http://dlinsin.blogspot.com/2008/01/wonderful-checked-exceptions.html" rel="nofollow noreferrer">including myself</a> - is trying to avoid checked Exceptions. They are trying to work their way around being forced to catch Exceptions by using <a href="http://java.sun.com/javase/6/docs/api/java/lang/RuntimeException.html" rel="nofollow noreferrer">RuntimeExceptions</a>.</p>\n'}, {'answer_id': 1909, 'author': 'Patrick Johnmeyer', 'author_id': 363, 'author_profile': 'https://Stackoverflow.com/users/363', 'pm_score': 2, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/questions/1104/#1109">Chris</a>\' probably has the best pure answer to the question:</p>\n\n<p>However, I\'m curious about the root of the question.  If the user should <em>always</em> wrap the call in a try/catch block, should the user-called function really be throwing exceptions in the first place?</p>\n\n<p>This is a difficult question to answer without more context regarding the code-base in question.  Shooting from the hip, I think the best answer here is to wrap the function up such that the recommended (if not only, depending on the overall exception style of the code) public interface does the try/catch <em>for</em> the user.  If you\'re just trying to ensure that there are no unhandled exceptions in your code, unit tests and code review are probably the best solution.</p>\n'}, {'answer_id': 126184, 'author': 'Scott Langham', 'author_id': 11898, 'author_profile': 'https://Stackoverflow.com/users/11898', 'pm_score': 3, 'selected': False, 'text': "<p>You shouldn't be using an exception here. This obviously isn't an exceptional case if you need to be expecting it everywhere you use this function!</p>\n\n<p>A better solution would be to get the function to return an instance of something like this. In debug builds (assuming developers exercise code paths they've just written), they'll get an assert if they forget to check whether the operation succeded or not.</p>\n\n<pre><code>class SearchResult\n{\n  private:\n    ResultType result_;\n    bool succeeded_;\n    bool succeessChecked_;\n\n  public:\n    SearchResult(Result&amp; result, bool succeeded)\n      : result_(result)\n      , succeeded_(succeeded)\n      , successChecked_(false)\n    {\n    }\n\n    ~SearchResult()\n    {\n      ASSERT(successChecked_);\n    }\n\n    ResultType&amp; Result() { return result_; }\n    bool Succeeded() { successChecked_ = true; return succeeded_; }\n}\n</code></pre>\n"}, {'answer_id': 37101290, 'author': 'Trevor Hickey', 'author_id': 908939, 'author_profile': 'https://Stackoverflow.com/users/908939', 'pm_score': 1, 'selected': False, 'text': '<p>There was once an attempt to add <a href="http://en.cppreference.com/w/cpp/language/except_spec" rel="nofollow">dynamic exception specifications</a> to a function\'s signature, but since the language could not enforce their accuracy, they were later depreciated. </p>\n\n<p>In C++11 and forward, we now have the <a href="http://en.cppreference.com/w/cpp/language/noexcept_spec" rel="nofollow">noexcept specifier</a>.<br>\nAgain, if the signature is marked to throw, there is still not requriement that it be handled by the caller.  </p>\n\n<hr>\n\n<p>Depending on the context, you can ensure that exceptional behaviour be handled by coding it into the type system.  </p>\n\n<p><strong>See:</strong> <a href="http://en.cppreference.com/w/cpp/utility/optional" rel="nofollow">std::optional</a> as part of the library fundamentals.</p>\n'}]
['https://Stackoverflow.com/questions/1104', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/236/']