Pergunta

I've been trying to write my own custom constructor, but getting error about base() constructor. I've also been searching how to solve this error, but found nothing and all the examples round the internet are showing almost the same code as mine.

Whole Exception.cs content:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RegisService
{
public class Exceptions : Exception
{        
}

  public class ProccessIsNotStarted : Exceptions
  {
      ProccessIsNotStarted()
          : base()
      {
          //var message = "Formavimo procesas nestartuotas";
          //base(message);
      }

      ProccessIsNotStarted(string message) 
          : base(message) {}

      ProccessIsNotStarted(string message, Exception e)
          : base(message, e) {}
  }
}

first overload with base() is working, no errors were thrown. Second and the third overloads are telling me that :

"RegisService.Exceptions does not contain a constructor that takes 1(2) arguments"

One more way I've been trying to solve the error:

ProccessIsNotStarted(string message)              
    {
        base(message);
    }

    ProccessIsNotStarted(string message, Exception e)
    {
        base(message, e);
    }

this time, VS is telling me that:

"Use of keyword 'base' is not valid in this context"

So, where is the problem? Looks like the base() constructor has some weird overloads or I'm calling it in inappropriate way?

Foi útil?

Solução

Your Exceptions class needs to define all constructors you want to provide. The constructors of System.Exception are not virtual or abstract. The keyword base does not call the members of all base classes, but of the one base class you provide in the class declaration. Take a look at this:

public class Exceptions : Exception
{
    public Exceptions(string message)
        : base(message) {}
}

public class ProccessIsNotStarted : Exceptions
{
    public ProccessIsNotStarted()
        : base()
    {
    }

    public ProccessIsNotStarted(string message) 
        : base(message)
    {
        // This will work, because Exceptions defines a constructor accepting a string.
    }

    public ProccessIsNotStarted(string message, Exception e)
        : base(message, e) 
    {
        // This will not work, because Exceptions does not define a constructor with (string, Exception).
    }
}

The parameterless constructor gets defined by default. To hide it you need to declare it private.

Regarding to the MSDN you should keep your exception inheritance hierarchy flat:

If you are designing an application that needs to create its own exceptions, you are advised to derive custom exceptions from the Exception class. It was originally thought that custom exceptions should derive from the ApplicationException class; however in practice this has not been found to add significant value.

You might also take a look at this page.

Outras dicas

Remove the Exceptions class entirely and let ProccessIsNotStarted inherit directly from System.Exception.

The constructors of a class are not automatically "copied" to derived classes; they are available using base, but you have to define them manually.

base refers to the immediate base class, not any base class down the chain. Your ProcessIsNotStarted class is a direct subtype of RegisService.Exceptions, not System.Exception. RegisService.Exceptions does not have a constructor with the signature (string, Exception) or (string).

Try adding the two constructors to your RegisService.Exceptions base class.

If you examine the following piece of code:

public class Exceptions : Exception
{        
}

You'll notice there are no constructors. Well, that's kind of a lie, because the default public constructor can be used, but there are no custom defined constructors.

If you want to expose the constructors of Exception through Exceptions then you're going to have to define them on Exceptions and call them using base from there, since inheriting exceptions calling base are calling Exceptions, hence Exception isn't their base and therefore the constructors not accessible.

public class Exceptions : Exception
{        
    Exceptions(string message) 
      : base(message) { }

    Exceptions(string message, Exception e)
      : base(message, e) { }
}

Then, you can do new Exceptions("", null) quite fine. And also, your base constructor calling when using inheritance.

Whether or not you get any value from this inheritance chain, I don't know, and you might want to take out the middle man, so to speak, as per another suggestion.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top