Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6663729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:31:52+00:00 2026-05-26T02:31:52+00:00

I have a control circuit that I communicate with via serial port. If the

  • 0

I have a control circuit that I communicate with via serial port. If the response command doesn’t match a certain format I consider it to be an error and was wondering if I should return an error code or throw an exception? For example:

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

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:31:52+00:00Added an answer on May 26, 2026 at 2:31 am

    In almost all cases I would communicate errors via exceptions. I would almost never use “error codes” as such – occasionally it’s useful to give success/failure along with a result as in int.TryParse etc, but I don’t think this situation works like that. This sounds like a real error condition which should stop further progress though, so an exception is appropriate.

    EDIT: As noted in comments, if the circuit reporting an error really is “expected” and the caller should be able to deal with it and should actively be looking for that situation, then it’s reasonable to use a status code.

    Unfortunately error handling is one of those things that we haven’t really got right in software engineering yet…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a library class CircuitController that communicates with a control circuit via
Some code that I don't have control over is overriding the global JSON object
I want to read UTF-8 strings from a server that I have control of,
First, I'll start off by saying that I do not have control over the
I have a control that look something like this: <asp:DetailsView ID=dvUsers runat=server DataSourceID=odsData DataKeyNames=Id>
I have a control circuit which has multiple settings and may have any number
I have control (implemented C#, .Net 2.0) that inherits from combobox. It has filtering
I have control in a page that gets html from text file and renders
I have some values coming from a datasource that I have control over. I
I have a block of html code that I don't have control over. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.