Search code examples
c#exceptionconceptual

Should I use Exception Handling or Check the values


I need some help with a conceptual problem.

Essentially, I have some data from a known source. I know that all of the data can potentially not have a value. I have two options.

Option 1: I can check all of the data before it is stored to prevent invalid cast exceptions

Option 2: I can let it throw an exception.

Now I know for a fact that it is best practice to use a proactive error handling method and check for nulls; however, I am sure that out of a million mappings from tables to objects I will have no more than 2 errors. Should I throw an exception or do the check?

People generally say to do the check because the exception takes more resources than a simple check; however, is this the case when you need to do over a million checks verses 1 exception?


Solution

  • Exceptions should be used in exceptional cases. Two out of a million sounds exceptional to me.