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.