Custom exceptions and basic constructor

I am trying to write my own constructor but I am getting an error about the constructor base()

. I also searched for how to solve this error but didn't find anything and all the examples on the internet show almost the same code as mine.

Content of all Exception.cs:

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) {}
  }
}

      

the first overload with base()

works, there were no errors. The second and third overloads tell me that:

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

Another way I tried to solve this error:

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

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

      

this time VS tells me that:

"The use of the base keyword is invalid in this context."

So where is the problem? It looks like the constructor base()

has some weird overloads or am I calling it in an inappropriate way?

+3


source to share


4 answers


Your class Exceptions

should define all the constructors you want to provide. Constructors System.Exception

are not virtual or abstract. The keyword base

does not call the members of all base classes, but only one base class that you specify 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 is the default. To hide it, you need to declare it private

.



With respect to MSDN, you should stick to the exception inheritance hierarchy:

If you are developing an application that needs to throw its own exceptions, it is recommended that you get custom exceptions from the Exception class. Originally, it was assumed that custom exceptions should be thrown from the ApplicationException class; however, in practice, this did not find significant significance.

You can also take a look at this page .

+6


source


Remove the class Exceptions

and ProccessIsNotStarted

inherit directly from System.Exception

.



Class constructors are not automatically copied to derived classes ; they are available with base

, but you must define them manually.

+1


source


Base

refers to an immediate base class, not a chained base class. The ProcessIsNotStarted class is a direct subtype of RegisService.Exceptions, not System.Exception. RegisService.Exceptions does not have a constructor with signature (string, exception) or (string).

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

+1


source


If you consider the following piece of code:

public class Exceptions : Exception
{        
}

      

You will notice that there are no constructors. Well, such a lie because you can use the standard default constructor, but there are no user-defined constructors.

If you want to expose constructors Exception

through Exceptions

, then you will need to define them on Exceptions

and call them using base

from there, since inheritance of exceptions, throwing base

, calling Exceptions

, is therefore Exception

not theirs base

, and therefore constructors are not available.

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 are getting any value from this chain of inheritance, I don't know, and you might want to pull the middle person out, so to speak, according to another suggestion.

+1


source







All Articles