Personally, I like to deal with this situation by defining some kind of response object. I came to this through the experience of this and weighing what should be considered exceptional. If you interact with any device and the user turns off the device or something like that, this is an exceptional condition, and you should throw an exception.
If calling a specific method on a device fails or something else, this is not exceptional, but it is an error. I do not want to determine the magic number if I return a double, and I do not want to throw an exception. So, I define something like:
class DeviceResult //could also be struct { public double ReturnValue { get; set; } public bool IsValid { get; set; } public string ErrorMessage { get; set; } }
The features here are not very important - you adapt them to what the clients of your method want. In this example, the client API must verify that it is correct and, if so, use the return value - there is no magic number and no exception. The paradigm is important. The way I see it, you are using an object-oriented language so you can use objects. :)
source share