Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Exception use instead of validation

Exception use instead of validation

Scheduled Pinned Locked Moved C#
designtestingbeta-testingquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Raybarg
    wrote on last edited by
    #1

    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 with CurrencyManager and BindingContext. 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?

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups