tags:

views:

497

answers:

7
+4  Q: 

Exceptions in C++

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 it using try/catch (unlike in Java).

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

+11  A: 

No; see A Pragmatic Look at Exception Specifications for reasons why not. 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.

Chris Jester-Young
A: 

Or you could start throwing critical exceptions. Surely, an access violation exception will catch your users' attention.

Ryan Fox
+2  A: 

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 c[++], you dont have to catch an unchecked exception.

For a good reference try this

shsteimer
A: 

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

I find it rather funny, that the Java crowd - including myself - is trying to avoid checked Exceptions. They are trying to work their way around being forced to catch Exceptions by using RuntimeExceptions.

david
+1  A: 

Chris' probably has the best pure answer to the question: http://stackoverflow.com/questions/1104/#1109

However, I'm curious about the root of the question. If the user should always wrap the call in a try/catch block, should the user-called function really be throwing exceptions in the first place?

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 for 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.

Patrick Johnmeyer
A: 

@Patrick

Consider the following scenario....

We have a data-structure which can hold any integer. Now we need to search a given integer in this data structure. At some point our team decided that we should throw exceptions if the integer doesnt exist in the structure. We are not worried about the performance hit due to this as this would be one in a million case.

Over time, we have seen that developers forget to catch the Data_Not_Found exception. So from there the questions arises.

Regards, Sachin

PS: I know that we could have used many other ways to tackle the problem :(

sachin
If you find a way to make people not "forget" to handle errors, I think you would be a billionaire.
Greg Rogers
I wish! I think my answer suggests a method that does just this. I can't claim it's mine though... I read it in a C++ book somewhere.
Scott Langham
+3  A: 

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!

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.

class SearchResult
{
private:
   ResultType result_;
   bool succeeded_;
   bool succeessChecked_;
public:
   SearchResult(Result& result, bool succeeded)
     : result_(result)
     , succeeded_(succeeded)
     , successChecked_(false)
   {
   }
   ~SearchResult()
   {
       ASSERT(successChecked_);
   }
   ResultType& Result() { return result_; }
   bool Succeeded() { successChecked_ = true; return succeeded_; }

}

Scott Langham
+1 to that. If you expect some result, it shouldn't be returned in form of an exception.
macbirdie
GCC at least has a function attribute to require handling the return value. Less messy than this extra bool.
Zan Lynx