I like to verify that all method parameters have the correct information before doing anything with them. Something like that:
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"); } ...
However, in this post, someone comments that it is not recommended to throw an exception as a regular thread, but I'm not sure if it is so or not, because if I need to check the parameters, there are also exceptions like ArgumentNullException and ArgumentException , which it seems that it can be thrown when there is some kind of problem with the parameters, it makes me wonder if this is really a bad way to do, an example that I comment on.
Another reason another user gives is because the exception consumes 4000-8000 CPU cycles. So, the goal in my case is to know if there is some kind of error with the parameters, and indeed, if the application works as expected, the exception will never be thrown, so in practice, if the application has no errors, t decreases.
So, in general, I would like to know how to best handle parameter checking before continuing with the process.
Thanks.
source share