0

Running into a predicament. New to C# but trying to go through and catch all the exceptions that could be thrown. I am reading Values from a PLC using twincat ADS. If it cant find a symbol/Address it throws an exception. In my case I wrote a method

        public bool ReadBool(string Address)
    {
        return (bool)tcAds.ReadValue($"{Address}", typeof(bool));
    }

This will read either a true or false from the PLC depending on what the current value is. If I am not connected to the PLC or I typed the address it should be looking at wrong it will throw an Exception. Currently I just want to Write to Console "(Address) Could not be Found" without stopping the program execution.

             public bool ReadBool(string Address)
        {
            try
            {
                return (bool)tcAds.ReadValue($"{Address}", typeof(bool));
            }
            catch
            {
                // If we are Connected then Show Address
                if (ConnectionState == ConnectionStates.Connected)
                {
                    Trace.WriteLine($"{Address} Is not Found");
                    return null;
                }
                else
                {
                    Trace.WriteLine("Please try and Connect to PLC");
                }
                // If not Connected then Say Try Connections
            }

        }

Since my expected values CAN be either true or false I'm not sure how to keep program running in this case if I'm NOT able to read the value.

6
  • 3
    "New to C# but trying to go through and catch all the exceptions that could be thrown" - I understand why you might want to do this, it's all part of the learning experience - but in production-quality C# program code you should only catch exceptions that you can gracefully and correctly handle (i.e. never swallow exceptions). The reason for this is that exceptions are exceptional: and (more often than not) it's preferable (or even entirely correct) to write software that simply fails-fast instead of writing software that tries to be fault-tolerant via catch.
    – Dai
    Commented Jul 10 at 0:50
  • 2
    ...also, C# is not Java: Java's philosophy of checked-exceptions and a tradition of using exceptions for program flow control (which my rigid mind finds an offensive concept, really) - it's best to not try to write C# with Java-style exception throwing adn handling.
    – Dai
    Commented Jul 10 at 0:54
  • 1
    Adding to what Dai said: if you want to provide more detailed information to the caller, you can opt to wrap the exception in a more appropriate exception or a custom exception, and throw that. Wrapping it will retain the stack trace for the exception you caught, but add your information on top. Also see Microsoft's Best practices for exceptions. Commented Jul 10 at 1:04
  • Use Exceptions Exceptionally. You haven't mentioned what type of app this is, if it was a ConsoleApp you could do Console.ReadKey and wait for the user to try again. If its a Winform App you could exit out of the method with a MessageBox.Show. If users enter something wrong, or aren't connected they will have to do something about it before continuing. Commented Jul 10 at 1:27
  • @Dai Nor will you find mine throwing non-exceptional exceptions. That wasn't what I was suggesting... Commented Jul 10 at 2:54

0

Browse other questions tagged or ask your own question.