I have got a short design question to all of you. I have got a method which must be executed as fast as possible, but it also must give information about occurred errors by an exception.
It may happen that the function is called a thousand times within a loop, but exceptions will occurr very rarely (<1% of the values will cause an error). The function only computes very simple mathmatically operations and do not call any methods, using LINQ or something else.
Ok that's the situation so far, and the following describes the two scenario I have got to solve this method.
My own solution was the most common way, just check each parameter before the calculation (to prevent exceptions in the ramaining method):
int FastMethod(int number)
{
if (number <= 0)
{
throw new ArgumentOutOfRangeException();
}
// (...) more parameter validations
// do some operations with the number here
}
The other solution which was recommended to me was, running into the error and only re-throw the catched exception:
int FastMethod(int number)
{
try
{
// do some operations with the number here
}
catch (Exception ex)
{
throw (ex);
}
}
So what would you recommend?
The second scenario doesn't need to call all these if statements that may raise the performance, but it looks poor designed in my opinion. May you can teach me :)