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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:39:11+00:00 2026-05-14T02:39:11+00:00

What factors dictate throwing an exception if argument is null (eg if (a is

  • 0

What factors dictate throwing an exception if argument is null (eg if (a is null) throw new ArgumentNullException() ), as opposed to checking the argument if it is null beforehand.

I don’t see why the exception should be thrown rather than checking for null in the first place? What benefit is there in the throw exception approach?

This is for C#/.NET

Thanks

  • 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-14T02:39:12+00:00Added an answer on May 14, 2026 at 2:39 am

    Your method can do one of three things when an argument is null. It can throw an exception, it can return without doing anything, or it can make assumptions and attempt to continue. I’m assuming you are trying to choose between the first two options.

    Since the calling method can always check the arguments prior to calling your method it can prevent invalid values from being passed. This is true regardless of how your method handles invalid values.

    When your method is called with invalid arguments it should notify the caller that processing did not continue. You can notify it by throwing an exception or returning an error value. If you check for null and don’t return an error value the calling method will assume your method processed with no errors.

    How do you want the calling method to handle the case where your method does not process? If passing null is a normal occurrence and should be easily handled by the calling method returning an error on a null argument is acceptable. In this case the calling method either checks the arguments prior or the return value after the choice is up to whoever writes the calling method.

    If passing a null is very rare then throw an exception. The calling method can prevent the exception by checking arguments before calling your method just like above. By throwing an exception the call stack can be unwound without having to add lots of code.

    Summary:
    If an argument set to null is routine and the calling method should be written to handle your function returning without doing anything just return an error value. If a null argument is rare and the calling method can’t be expected to handle your method doing nothing throw an exception so the call stack is unwound.

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

Sidebar

Related Questions

What should be the factors considered while checking if webservice is available or running
What factors indicate that a project's solution should not be coded in a dynamic
What factors should I be aware of that can cause excessive stored procedure recompilation?
What factors should I consider before adopting the Knockout JavaScript library for my application?
I was surprised to see that R will coerce factors into a number when
Possible Duplicate: Human factors design (meeting psychological needs in UI design) What should a
What factors determine which approach is more appropriate?
What are some factors that would drive me away from using Tomcat in production
What are the factors in deciding framework to choose ,technology for a web application
I need to find out the prime factors of over 300 billion. I have

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.