0

Basically i know how and why we use ArgumentNullException. One thing that is not clear for me yet is when exactly do we need to use it.

Currently i think we need to use it when we write method with parameter and there is a possibility someone who use our code will pass a null parameter. This example is for when we write a library and someone else around the world use the code, or when we work on team.

What if we write code for our own, there is still possibility for null parameter but from the caller method we make sure that the param is never null (we know that it's should not be null since we are the one who write the method). Do we still need to use ArgumentNullException?

2
  • 1
    Check this - programmers.stackexchange.com/questions/121121/… Commented Nov 9, 2015 at 4:39
  • "we know that it's should not be null since we are the one who write the method" - In the words of Benny Hill - "Do not assume! It makes an ass out of you and me" ;) Commented Nov 9, 2015 at 6:08

1 Answer 1

4

(we know that it's should not be null since we are the one who write the method).

That sounds like famous last words. As a general rule you want to validate as much of the input at the beginning of every method. It's better to fail with an exact exception before beginning your operation than some mysterious side-effect exception later on.

And I always write my code as if it's going to eventually be a 3rd-party library used by a million people, even if I know only I (or a small group) will use it. There's no reason to be lazy, it's not even saving you anything. Over the long run you will save time in debugging later.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.