相关疑难解决方法(0)

永远可以退回例外吗?

我有读取各种传感器和过程数据的方法.在这些方法中,我有其他方法通过串口向电路发送命令(获取传感器值).可能会发生通信错误,我想知道是否可以"返回"异常?例如:

public double AverageSensorValues()
{
  try
  {
   ...
   double sensor1Value = SensorValue(1);
   double sensor2Value = SensorValue(2);
   ...
   }
   catch (Exception ex)
   {
      MessageBox.Show("Error in AverageSensorValues()");
   }
}

public double SensorValue(int sensorNum)
{
   try {

   // Send command to circuit to return value.
   string response = SendCommand(commandStringToGetValue);

   // Check to see if response is an error
   bool isError = ErrorReturned(response);

   if(isError)
      ProcessError(response);  // Throws exception.

   ... // Other things that could cause exceptions to be thrown.

   }
   catch (Exception ex)
   { …
Run Code Online (Sandbox Code Playgroud)

.net c# exception

2
推荐指数
1
解决办法
767
查看次数

标签 统计

.net ×1

c# ×1

exception ×1