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

The Archive Base Latest Questions

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

What would you consider to be the best exception type to throw when an

  • 0

What would you consider to be the best exception type to throw when an extension method is called on a null instance (where the extension method does not allow it)? Since extension methods are nothing but static methods you could argue that it should be ArgumentNullException, but on the other hand they’re used like instance methods so it might be more natural to use the NullReferenceException. Let’s take the following example:

public static string ToInvariantString(this IFormattable value, string format) {     return value.ToString(format, CultureInfo.InvariantCulture); } 

This way a NullReferenceException will be thrown if the value parameter is null.

The other example would be:

public static string ToInvariantString(this IFormattable value, string format) {     if (value == null) throw new ArgumentNullException('value');     return value.ToString(format, CultureInfo.InvariantCulture); } 

EDIT: In some of the answers you have pointed out that an extension methods can be called like a static method and in those cases a null reference exception would be wrong, which is a great point, and actually one of my concerns, not sure why I forgot to mention that in the question in the first place.

Someone also pointed out that it’s wrong to throw a NullReferenceException, and yes, it is. That’s why I don’t throw it, I just let it happen (let the CLR throw it) by not guarding the method.

I think I favor the ArgumentNullException (that’s what I’ve use so far) but I still think there is at least room to argue for an against the NullReferenceException since it seems more natural in most places where the method is going to be used.

  • 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. 2026-05-11T02:10:06+00:00Added an answer on May 11, 2026 at 2:10 am

    In general, exceptions included, you should treat an extension method as if it were a normal static method. In this case you should throw an ArgumentNullException.

    Throwing a NullReferenceException here is a bad idea for a few reasons

    • A null reference did not actually occur so seeing one is counterintuitive
    • Throwing a NullReferenceException and causing a NullReferenceException to occur produce discernably different exceptions (One way to see the difference is the error code). This is true of many exceptions that are thrown by the CLR.

    See When can you catch a StackOverflowException (a post I did on this subject).

    • It’s perfectly legal to call an extension method just as if it were a regular method. In that case I would certainly not except a NullReferenceException, but instead an ArgumentNullException.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 61k
  • Answers 61k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer In this article Five Steps to Managing Unstructured Data with… May 11, 2026 at 9:54 am
  • added an answer The closest you could do is something like: SELECT MY_TABLE.*,… May 11, 2026 at 9:54 am
  • added an answer I'm able to run an SQL query by piping it… May 11, 2026 at 9:54 am

Related Questions

What would you consider to be the best exception type to throw when an
What would you consider best practice for organizing JUnit tests in a project, and
Out of the following queries, which method would you consider the better one? What
What are the things that you would consider when defining indexes, clustered and non-clustered,
What would you recommend for OS X development of a graphical application like those
What would you recommend to search a sql server table (varchar(max) column) for a
What would you suggest would be the best operating system for a web server?
What would you recommend for class that needs to keep a list of unique
What would you suggest for the following scenario: A dozen of developers need to
What would you suggest as a replacement to the Maven Java build toolset? Just

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.