직렬 포트를 통해 통신하는 제어 회로가 있습니다. 응답 명령이 특정 형식과 일치하지 않으면 오류라고 생각하고 오류 코드를 반환해야하는지 또는 예외를 throw해야하는지 궁금합니다. 예 :C# 오류 코드 vs. 예외
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 // Some unknown error occurred...
{
... // Same question as above "if".
}
}
else // Everything is OK, proceed as normal.
...
}
감사합니다.
재미있는; 나는 당신이 표준 오류 처리를위한 예외를 추천하고 싶다는 것에 놀랐다. 비정상적이거나 예기치 않은 상황에 대한 예외를 예약하지 않겠습니까? –
@JohnWeldon : 아니요. 오류가 발생하여 호출 코드가 모든 것이 정상적으로 진행되지 않아야 함을 의미합니다. "* 응답 명령이 특정 형식과 일치하지 않으면 오류라고 생각합니다." –
즉프로그램의 정상적인 흐름은이 사건을 다루기위한 것이 아닙니다. 이 경우 예외 사용에 동의합니다. 그러나 프로그램이 정상적으로이 오류 조건에 응답 할 것으로 예상되는 경우 예외를 사용하지 않습니다. 그 질문에 대한 나의 해석은 후자이지만, 전자는 그럴 듯하다. –