4

string receiveFromServer();

this function returns a string that was received from some server. If there was an error along the way (including protocol), i want to return a NULL string. However, this doesn't work in c++ (unlike java). i tried:

string response = receiveFromServer();
if (response==NULL) {
    cerr << "error recive response\n";
}

but its not legal. Since an empty string is also legal to return, what can i return that will indicate the error?

thank you!

1
  • Is not receiving a response an error, or just the end of the data stream (expected and normal, but different)? Commented May 27, 2011 at 9:14

5 Answers 5

9

You can throw an exception to indicate an error.

try {
    std::string response = receiveFromServer();
} catch(std::runtime_error & e) {
    std::cerr << e.what();
}

Then you would need a throw std::runtime_error("Error receive response") somewhere in receiveFromServer().

It is often considered good practice (though some might disagree) to create your own exception-classes that derive from std::runtime_error to enable clients to catch your errors specifically.

Sign up to request clarification or add additional context in comments.

1 Comment

Good suggestion, but why runtime_error? I prefer my own exception types inheriting from std::exception (or some of it's derives)
5

You can either throw an exception (better way), or return boost::optional< string >, but then you have to check if the return value is valid (this is actually worse).

6 Comments

Instead of boost::optional, you can also use std::pair<string, bool>.
I was going to upvote until you said it was worse. How is that worse? It's what I would do (in fact, I was going to submit an answer showing how but checked for others first).
@GMan In c++ you should use exceptions for error handling. Checking the return value is for c. By using exceptions, you are using a hidden channel for error handling, making the code more readable. But you already know this, right? :)
@VJo: I asked a clarifying comment on the question, but I had it in my mind (indeed, not necessarily) that reaching the end of the data stream was not an error, just a different result. If it is in fact an error, it should be an exception, but if returning nothing is normal behavior, optional is the way to go.
@GMan But the question states that it is an error. For errors, throw an exception. For that reason, returning anything from the function is actually worse, because you would have to check the result of the function.
|
1

NULL has only a meaning when using pointers. In java strings are pointers so you can do that.

In C++, if you return an std::string, it must exist. So you have some possibilites

  • Throw an exception
  • Return a pair with a bool indicating the success
  • Return an empty string
  • Use a pointer (I strongly discourage that option)

Comments

1

Maybe you should try to handle with exceptions

 try
  {
    string response = receiveFromServer();
  }
  catch (...)
  {
    cerr << "error recive response\n";
  }

Comments

0

If you want to return an empty string you could also use the function string::empty() to test if it is empty

1 Comment

I don't know if the question added this after your answer, but... "Since an empty string is also legal to return, what can i return that will indicate the error?"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.