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 7710391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:06:50+00:00 2026-06-01T01:06:50+00:00

I know two ways to check parameters of the method and throw exceptions when

  • 0

I know two ways to check parameters of the method and throw exceptions when it is needed.

1) Check one each parameter and throw an exception when it is wrong:

public void Method(object parameter1, object parameter2)
{
    if (parameter1 == null)
    {
        throw new ArgumentNullException("parameter1");
    }

    if (parameter2 == null)
    {
        throw new ArgumentNullException("parameter2");
    }

    ...
}

2) Check all parameters at once and throw same exception for all:

public void Method(object parameter1, object parameter2)
{
    if (parameter1 == null || parameter2 == null)
    {
        throw new ArgumentNullException();
    }

    ...
}

The first approach is, in my opinion, better and cleaner, but also cover a lot of lines. For example, a method that actually do the 2 lines of code – in this way code will increase by 4 rows (including blank line) for each parameter.

I am interested in is the approach used by experienced programmers. Are there better ways than these two?

  • 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-06-01T01:06:51+00:00Added an answer on June 1, 2026 at 1:06 am

    Updated July 2020

    Check out this blog post on how you can achieve a similar approach to Code Contracts.

    https://enterprisecraftsmanship.com/posts/code-contracts-vs-input-validation/

    Original answer provided below

    —-

    If you are using .NET framework 4, check out Code Contracts, which simplifies it down to a single line of code

    public string Reverse(string text)
    {
       Contract.Requires<ArgumentNullException>(text!=null, "ParAmeter cannot be null.");
      
       .....
    }
    

    The reason you would use this is because you can now get automated tools like Pex to tell you what unit tests to apply to this method. It also gives you feedback at compile time if this method would throw an exception based on how you are calling it. Like

    String text = null;
    String reversedString = Reverse(text);
    

    The compiler will warn you that this will throw an exception.

    Note Code Contracts needs an add-in to be installed, but it is free.

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

Sidebar

Related Questions

I know that we can initialise an array in one of two ways: Loop
Java question: As far as I know, there are two ways to check inside
So far I know two ways to pass php variables to javascript. One is
I'm just curious on this one. I know two ways of defining events in
Currently I know of only two ways to cache data (I use PHP but
Ok , I know the two standard ways to create a new thread and
I know two approaches to Exception handling, lets have a look at them. Contract
I know of two methods of setting a default parameter, but I would like
I am a little confused in declaring variable in android, I know two ways
I know two ways to set the timeout interval of a session: way 1:

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.