从函数c ++返回NULL字符串

时间:2021-04-10 09:56:16

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:

此函数返回从某个服务器接收的字符串。如果一路上出现错误(包括协议),我想返回一个NULL字符串。但是,这在c ++中不起作用(与java不同)。我试过了:

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!

5 个解决方案

#1


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().

那么你需要在receiveFromServer()中的某个地方抛出std :: runtime_error(“错误接收响应”)。

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.

通常认为好的做法(尽管有些人可能不同意)来创建自己的异常类,这些异常类派生自std :: runtime_error,以使客户端能够专门捕获您的错误。

#2


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

你可以抛出异常(更好的方式),或者返回boost :: optional ,但是你必须检查返回值是否有效(这实际上更糟)。

#3


1  

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

使用指针时,NULL只有一个含义。在java中,字符串是指针,所以你可以这样做。

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

在C ++中,如果返回std :: string,它必须存在。所以你有一些可能性

  • Throw an exception
  • 抛出异常

  • Return a pair with a bool indicating the success
  • 返回一对bool表示成功

  • Return an empty string
  • 返回一个空字符串

  • Use a pointer (I strongly discourage that option)
  • 使用指针(我强烈反对该选项)

#4


1  

Maybe you should try to handle with exceptions

也许你应该尝试处理异常

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

#5


0  

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

如果要返回空字符串,还可以使用函数string :: empty()来测试它是否为空

#1


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().

那么你需要在receiveFromServer()中的某个地方抛出std :: runtime_error(“错误接收响应”)。

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.

通常认为好的做法(尽管有些人可能不同意)来创建自己的异常类,这些异常类派生自std :: runtime_error,以使客户端能够专门捕获您的错误。

#2


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

你可以抛出异常(更好的方式),或者返回boost :: optional ,但是你必须检查返回值是否有效(这实际上更糟)。

#3


1  

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

使用指针时,NULL只有一个含义。在java中,字符串是指针,所以你可以这样做。

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

在C ++中,如果返回std :: string,它必须存在。所以你有一些可能性

  • Throw an exception
  • 抛出异常

  • Return a pair with a bool indicating the success
  • 返回一对bool表示成功

  • Return an empty string
  • 返回一个空字符串

  • Use a pointer (I strongly discourage that option)
  • 使用指针(我强烈反对该选项)

#4


1  

Maybe you should try to handle with exceptions

也许你应该尝试处理异常

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

#5


0  

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

如果要返回空字符串,还可以使用函数string :: empty()来测试它是否为空