How to Overload a constructor and also call the base class constructor in C#?

Asked

Viewed 196 times

0

I am creating a C# error that should extend the class Exception but, should also add an extra property, thus:

public class CampoVazioErro: Exception
{
    public string campo { get; }

        public CampoVazioErro(object c)
        {
            campo = c;
        }
        public CampoVazioErro(object c, string m) : base(m)
        {
        }
        public CampoVazioErro(object c, string m, Exception i) : base(m, i)
        {
        }
}

Is it possible I call the base class builder Exception and also call the simplest builder in my class CampoVazioErro(object c)? In my opinion there must be some way to do this but I’m not finding on the internet.

Note: I know to call another builder within the same class I just have to pass the this() in front of another manufacturer (ex: CampoVazioErro(object c, string m): this(c) but I have no idea what to call it and also the builder of my superclass.

  • You would have to call the "simple" builder of your class with the : this(c) and then the simple constructor would have called the base constructor using the : base()

1 answer

1


Exception classes should always end with the word Exception. You should inherit from ApplicationException and not Exception. In fact the latter should be forgotten by developers in general. And even for this I consider the third signature a mistake.

In general you should not use object as kind of nothing. This is practically a legacy of language.

These constructors have no direct relationship with the base constructors. Just warning if you think you have, not different signatures.

using System;

public class Program {
    public static void Main() {
        try {
            throw new CampoVazioException<int>(1);
        } catch (CampoVazioException<int>) {
            Console.WriteLine("deu erro");
        }
    }
}

public class CampoVazioException<T>: ApplicationException {
    public T Objeto { get; }

    public CampoVazioException(T objeto) : this(objeto, "") {}
    public CampoVazioException(T objeto, string mensagem) : base(mensagem) {
        Objeto = objeto;
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • Maniero, when you talk about class Exception, saying that deveria ser esquecida por desenvolvedores em geral. What is the reason? Is there any reference you could add on the subject?

  • 2

    It may be useful here and there, but people use it as a lazy way of making an exception. If you’re gonna do it this way, you might not want to make an exception. One of the main reasons the exception was created is to give a lot of information about the error, and especially what the error is. Every time someone throws or captures an exception is doing using exception the wrong way (except for the exceptions of this rule, I can’t resist the puns :) ). Some people even say this, and even the ApplicationException and some other "high-level" exceptions should be abstract and not allow inheritance.

  • 2

    This class was created to give the overall exception infrastructure, not for developers to use directly. In some cases the capture of it is useful, in general near the application entry point, when everything else failed and will log in error before application break.

  • Quite enlightening. It is a philosophy/good practice that will direct to a clean code. Thank you!

  • On the nomenclature of classes, I do not agree with the statement, since it is not an obligation of language but only a good practice in general. About the class ApplicationException, I will begin to use it because I agree with the concept.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.