views:

66

answers:

4

I have opended a question about repeated parameter-checks in public methods.

The result there seemed clear to me but out of this I have another question concerning these parameter checks (I had the same question in mind posting my last question but did the formulation awkward and wanted too much (more than one question in one post), I hope this time it’s better):


If I have repeating parameters that will be given to public Methods of a class of mine, the parameters must be validated in each public Method. If I delegate the check to another (private) method such as

void EnsureIndexIsValid(int index){
     if(index <0 || index > A_VALUE){
         throw new IndexOutOfRangeException(“..message..”);
     }
}

then the throwing method is EnsureIndexIsValidIndex and not the called method. Is this bad practice?

If its bad practice, one possibility would be to catch the exception in the using method and then re-throw the exception. Is this good practice (The source of the exception for the caller is now AMethod, not the EnsureIndexIsValid )? Or is it only overhead?

public void AMethod(int index,....){
  try{
      EnsureIndexIsValid(index)
  }catch(IndexOutOfRangeException e){
      throw e;
  }
  // ....

EDIT: The following seems definitivey not to be a good idea

If this would be good practice, would it be legitimate to catch a general exception for more of these checks and then re-throw:

public void AMethod(int index,....){
  try{
      EnsureIndexIsValid(index);
      EnsuresValueIsInValidRange(anotherParamter);
  }catch(Exception e){
      throw e;
  }
  // ...

Conclusion:

For all that come to this thread, here the short answer:

Delegation of parameter-checks seems to be a common pattern and is reasonable. However re-throwing such exceptions to hide the validation method is not clever. Better return a boolean value from the validation method and then throw the exception in the public method if the check fails.

+1  A: 

You should not be catching Exception, just the ones you can deal with so you shouldn't use your last example.

ChrisF
I'm aware that catching Exception is not very good (and if I ask about good practice I think its double as bad), but I thought that in the case that only parameter validation is done, it would not be so evil. If a stackoverflow exception will take place, the catch will not catch and the other exceptions are not probable, but you're right, it's definitively not very nice. So the extension is not a good idea. But what about the main question about re-throw the exception
HCL
@happyclicker - The edit was a good idea. Personally I'm not a big fan of rethrowing exceptions. You can always check the call stack to see which method called your validation code.
ChrisF
+1  A: 

No I don't think it's bad practice to delegate validation to another method, and I wouldn't re-throw exceptions from higher-level methods just to give that impression. The client doesn't really care where an exception comes from, just why it has been thrown.

Lee
+2  A: 

Hi,

I'd say it's actually good practice to throw the exception in the validation method as that's the location where the problem occurs (the validation fails). And if you need to know who called the validation method the exception will provide that information in its callstack. So I would not catch and rethrow the validation exception.

HTH.

andyp
+2  A: 

I would much rather have the private method be bool IndexIsValid, then the actual public method looks like

if(!IndexIsValid(parameterToThisMethod))
{
    throw new ArgumentOutOfRangeException("parametersToThisMethod");
}

This has the advantage of having the actual method at the top of the call stack.

Also, even if you did it the other way (with the private validator method throwing), this pattern:

catch(IndexOutOfRangeException e)
{
    throw e;
}

doesn't do nice things to the call-stack at all; either simply throw;, or throw new IndexOutOfRangeException("parameter", e);

AakashM