I like to check that all the parameters of the method have the correct information before doing something with them. Something like this:
public method(MyType param1)
{
try
{
if(param1 == null)
{
throw new ArgumentNullException("Error1");
}
if(param1.Property1 == null)
{
throw new ArgumentNullException("Error2");
}
if(param1.Property1.Amount <= 0)
{
throw new ArgumentException("Error3");
}
...
//Do what I need with the parameter
}
catch { throw; }
}
However, in this post someone comments that it is not a good idea to throw an exception as normal flow, but I am not sure if this is the case or not, because if I have to check the parameters and also there are exceptions like ArgumentNullException and ArgumentException that it seems that it can be thrown when there are some problem with parameters, it makes me to wondering if really it is a bad way to do, the example that I comment.
Another reason that another user gives it is that an exception consume 4000-8000 cycles of CPU. Well, the objective in my case it is to know if there are some error with the parameters and really if the application works as expected, the exception will never be thrown, so in practice, if the application has no bugs then the performance doesn't decrease.
So in summary, I would like to know how is the best way to handle the check of the parameters before continuing with the process.
Thanks.