Exception use instead of validation
-
Hi again, more questions. In my project I have this service layer I am using, to simplify it could be like this:
ServiceClient myService = new ServiceClient();
SomeDataType[] result = myService.GetSomeData(...arguments...);
myService.Close();It's 3rd party layer and lacks proper documentation so I played around with this. With some arguments the result is
null
. I already have my application use these results in grids and withCurrencyManager
andBindingContext
. The design is assuming that these results from service methods should never be null (but what if they are). So I decided to do "helper" class which will contain all those service calls and throw exception if data is not valid instead of checking if the data is valid outside.ServiceClient myService = new ServiceClient(); SomeDataType\[\] result; try { result = myService.GetSomeData(); if (result == null) { throw new ArgumentNullException("result", "Service method GetSomeData() returned null."); } } finally { myService.Close(); } return result;
Because the service raises no exception in the testing case I had it return null, it just returned null, though it has to be a case of an exception because it never shouldnt. So would this be the "proper" way to make sure I get the exception when I think the underlying layer (service) should have raised it?