Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从不同的函数中收集错误?_C#_Error Handling - Fatal编程技术网

C# 如何从不同的函数中收集错误?

C# 如何从不同的函数中收集错误?,c#,error-handling,C#,Error Handling,我正在做一个关于C#的项目。这是一个使用GSM调制解调器发送消息的项目。在我的项目中,我有几个函数可能会出错。我使用了一个字符串变量来存储错误消息。基本流程是这样的 class SMS{ string message; public string getport(){ if(ErrorCondition) message="Error Finding port"; return port;

我正在做一个关于C#的项目。这是一个使用GSM调制解调器发送消息的项目。在我的项目中,我有几个函数可能会出错。我使用了一个字符串变量来存储错误消息。基本流程是这样的

class SMS{
      string message;
      public string getport(){
         if(ErrorCondition)
            message="Error Finding port";
         return port;         
      }
      public void executecommand(command){
         if(ErrorCondition1)
            message="Error Executing Command";
         else(ErrorCondition2)
            message="No Response from device";         
      }
      public void sendMessage(int number, string text, out string error){
           findport();
           executecommand(command);
           error = message;
      }
}
注意:这不是工作代码

这是我一直想收集错误消息的方法,但我不确定是否正确。我需要你的帮助。有更好的方法吗?错误字符串不保存任何字符串。 更新:由于异常的答案即将到来,我对try-catch的安排感到困惑,这里我有我的实际代码,以便我能提供进一步的建议

public string ReadResponse(SerialPort port, int timeout)
{
    string buffer = string.Empty;
    try
    {
        do
        {
            if (receiveNow.WaitOne(timeout, false))
            {
               string t = port.ReadExisting();
               buffer += t;
            }
            else
            {
               if (buffer.Length > 0)
                   message = "Response incomplete";
               else
                   message = "Error on Modem";
             }
     }
     while (!buffer.EndsWith("\r\nOK\r\n") && !buffer.EndsWith("\r\n> ") && !buffer.EndsWith("\r\nERROR\r\n"));
 }
 catch (Exception ex)
 {
     throw ex;
 }
 return buffer;
}
你可以申报

string message;

私有列表消息;
并将消息添加到列表中,这也将有助于捕获多个错误。也要将其设置为私有,以便其他类无法访问该类。

您可以声明

string message;

私有列表消息;
并将消息添加到列表中,这也将有助于捕获多个错误。同时将其设置为私有,以便其他类无法访问相同的代码。

您的(伪)代码可以进行重构,以使用事实上的方法来管理C#中的错误,这些错误属于例外情况。基于您的代码,您习惯于使用特殊变量或返回值作为错误消息,这没有什么错,只是其他C#开发人员所期望的

下面是一些(伪)代码,演示了我键入的内容

class SMS {
    public string GetPort() {
        if (ErrorCondition) {
            throw new PortNotFoundException("Error finding port."); 
        }
        return port; 
    }

    public void ExecuteCommand(Command command) {
        if(ErrorCondition1) {
            throw new UnknownErrorException("Error Executing Command");
        }
        else(ErrorCondition2) {
            throw new NoResponseException("No Response from device"); 
        }
    }

    public void SendMessage(int number, string text) {
        FindPort();
        ExecuteCommand(command); 
    }
}
您将注意到,
SendMessage
中没有
out
变量来处理将错误传递给调用代码。这是由抛出的异常处理的。调用代码可以自由决定如何最好地处理异常

您的(伪)代码可以进行重构,以使用事实上的方法来管理C#中的错误,这是一种例外。基于您的代码,您习惯于使用特殊变量或返回值作为错误消息,这没有什么错,只是其他C#开发人员所期望的

下面是一些(伪)代码,演示了我键入的内容

class SMS {
    public string GetPort() {
        if (ErrorCondition) {
            throw new PortNotFoundException("Error finding port."); 
        }
        return port; 
    }

    public void ExecuteCommand(Command command) {
        if(ErrorCondition1) {
            throw new UnknownErrorException("Error Executing Command");
        }
        else(ErrorCondition2) {
            throw new NoResponseException("No Response from device"); 
        }
    }

    public void SendMessage(int number, string text) {
        FindPort();
        ExecuteCommand(command); 
    }
}

您将注意到,
SendMessage
中没有
out
变量来处理将错误传递给调用代码。这是由抛出的异常处理的。调用代码可以自由决定如何最好地处理异常

从sendmessage函数返回消息时也存在此问题,因为在send message中使用的是out string error,它应该是out List error。从sendmessage函数返回消息时也存在此问题,因为在send message中使用的是out string error,你能建议我进一步查看更新答案吗?你能建议我进一步查看更新答案吗