相关疑难解决方法(0)

C#错误代码与异常

我有一个控制电路,我通过串口进行通信.如果响应命令与某种格式不匹配,我认为它是一个错误,并想知道我是否应该返回错误代码或抛出异常?例如:

public double GetSensorValue(int sensorNumber)
{
   ...
   string circuitCommand = "GSV,01," + sensorNumber.ToString();   // Get measurement command for specified sensor.
   string responseCommand;
   string expectedResponseCommand = "GSV,01,1,OK";
   string errorResponseCommand = "ER,GSV,01,1,62";

   responseCommand = SendReceive(circuitCommand); // Send command to circuit and get response.

   if(responseCommand != expectedResponseCommand) // Some type of error...
   {
      if(responseCommand == errorResponseCommand) // The circuit reported an error...
      {
         ...  // How should I handle this? Return an error code (e.g. -99999) or thrown an exception?
      }
      else   // …
Run Code Online (Sandbox Code Playgroud)

c# error-handling

6
推荐指数
1
解决办法
6329
查看次数

标签 统计

c# ×1

error-handling ×1