0

What is best possible way to handle Exception in Extension methods? That includes exception generated from method and also null exception caused by calling extension method on null object.

Is it good to handle before calling extension method in proper business functionality or method should take care about exception.

Just to give idea here is code sample

class ABC
    {
        private readonly string name;
        public ABC(string name)
        {
            this.name = name;
        }

        public string Name { get { return this.name; } }
    }

    internal static class ABCExtension
    {
        internal static void GetName(this ABC abc)
        {
            Console.WriteLine(abc.Name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ABC abc = new ABC("baam");
            abc = null;
            abc.GetName();

            Console.Read();
        }
    }

This will generate null exception because abc object is null.

Adding details for more clarification here. I am confused between responsibility of class

I wanted to know whose responsibility is to handle exception. Extension class which is providing method or class which is using it?

Please let me know if any further details needed.

kunjee
  • 2,739
  • 1
  • 23
  • 38

3 Answers3

3

Arguments passed to extension methods, are like argument to regular methods. This included the first argument of the extension method (with this before the type name). If your method enforces any rule on the input parameters, it should check for them. And if there is a condition that should be handled by calling method, let the exception raise to the calling method.

By the way, alwasy check for nulls and don't let NullReferenceException happen. NullReferenceException means that there is a bug in your code.

Mohammad Dehghan
  • 17,853
  • 3
  • 55
  • 72
1

Normally, in a method like GetName(), you would check the parameter, and if null, throw a NullArgumentException. However, if GetName() were a member function, then abc.GetName() would cause a NullReferenceException. So, in the case of an extension method, it should emulate a member function, and throw a NullReferenceException

James Curran
  • 101,701
  • 37
  • 181
  • 258
  • I just give hvd a comment for more clarification. I repeat it here. "so, you mean to say that. Any type of exception should be thrown extension method but catching must be handled from the class that is using it?" is this a correct way? – kunjee Jan 26 '13 at 14:24
  • No, it shouldn't. `GetName` doesn't know whether it's called as `abc.GetName()` or as `ABCExtension.GetName(abc)`. In addition, there are extension methods that specifically *do* allow a first argument of `null`, so the user already needs to understand the difference. –  Jan 26 '13 at 14:24
  • Too late to edit it in my previous comment, but http://stackoverflow.com/questions/463302/argumentnullexception-or-nullreferenceexception-from-extension-method is relevant here. –  Jan 26 '13 at 14:30
  • @hvd can you please add details as different answer? So, I can close this question. :) – kunjee Jan 26 '13 at 14:31
  • @kunjee My comments aren't answers to your question :) I recommend accepting MD.Unicorn's answer. –  Jan 26 '13 at 14:33
0

There is not difference how you should handle and deal with exceptions in extension methods like in any other methods. Make sure that you catch exceptions outside (using try/catch) and that you validate parameters in you methods (using ArgumentException).

Carsten Schütte
  • 4,408
  • 1
  • 20
  • 24
  • Basically I asked because, I wanted to know whose responsibility is to handle exception. Extension class which is providing method or class which is using it? – kunjee Jan 26 '13 at 14:08
  • 2
    @kunjee *Can* the class which is using it handle an exception? Consider `Console.Write("The name is"); Console.WriteLine(abc.Name);` -- you'd get the `NullReferenceException` *after* partial output has already been written. So you'd need to make your extension method throw an exception beforehand if you want the caller to be able to handle it. –  Jan 26 '13 at 14:18
  • @hvd so, you mean to say that. Any type of exception should be thrown extension method but catching must be handled from the class that is using it? – kunjee Jan 26 '13 at 14:22
  • @kunjee That's pretty much what's in this answer, although one note: yes, the extension method should check that `abc` is not null, and throw an exception if it is. But the caller should ideally make sure not to pass a null reference in the first place, and if it can be sure of that, it doesn't need to catch any exception. –  Jan 26 '13 at 14:26