Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 922845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:05:50+00:00 2026-05-15T19:05:50+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:05:51+00:00Added an answer on May 15, 2026 at 7:05 pm

    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);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about how to collaborate auto complete with UI dialog: There
I'm beginner in SWT and I have a question about get values between differents
I have a client/server question that i am trying to figure out the best
I have a question about remote threads.I've read Mike Stall's article present here: <
Hello I have question about android spinner I have spinner which is populated by
Hello I have question about android spinner I have spinner which is populated by
I can switch between windows with C-x o, but if I have opened multiple
Have you ever seen any of there error messages? -- SQL Server 2000 Could
This is a basic question about navigation controller, however, I don't seem to get
Yes, it's a noob question... I have been using Dev-C++ for all my projects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.