Question

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?

Was it helpful?

Solution

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

OTHER TIPS

It is depending on the error kind if the error unexpectedly occur and it break the process workflow and a clean up is required. In this case you have to throw the exception. If the error is expected and the process can be continued you should never throw an exception in this case.

IMPORTENT Exceptions shall happening only in exceptional caeses and not in normal workflow.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top