Results 1 to 3 of 3

Thread: [RESOLVED] Where to throw Custom Exception

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    Resolved [RESOLVED] Where to throw Custom Exception

    I am pulling a starting date out of a table in the database. The field is a text field in an Access DB. I created a function to validate the date.
    Code:
            public bool IsDate(String testDate)
            {
                DateTime dtTest;
                return DateTime.TryParse(testDate, out dtTest);
            }
    This works and returns to me true or false. I am trying to figure out where the best place to use this code, in the function(FindStartDate) that returns the date? Or after the function is called like the following?
    Code:
                            String DateRangeStart = FindStartDate(strComp);
                            if (util.IsDate(DateRangeStart) == false)
                            {
                                ....
                            }
    I think either way will work but which way is a better design?

    Thanks.

  2. #2
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    259

    Re: Where to throw Custom Exception

    It really depends, if you just want to show a message or something along those lines, you wouldn't throw an error, you might log it for future reference but you would popup a messagebox or print to the screen the data isn't in the correct format, then allow them to correct it. You normally only throw exceptions when it is something that will stop the operation from completing and it is something that is hard to recover from. This is more of my believe and others will have their own opinions what are just as valid, probably not as good as mine , but still valid.

    In this case, I would look at validating the data BEFORE it gets into the database. What good is bad data in the database and THEN throwing exceptions it is bad? It would be helpful to see what FindStartDate does but I assume it get the info from the database, in which case, I would leave it as you have shown. I am a believer that a method should do a single thing.
    Last edited by wakawaka; Jan 17th, 2013 at 09:52 AM.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    Re: Where to throw Custom Exception

    This is an automated process that will be happening over night. If an error occurs I just want to exit the process cleanly, log the message and eventually send an email to myself. I don't control how the date gets into the database(notice it is a text and not a date field!) so I want to always validate it. I will validate the date outside the FindStartDate function.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width