Search code examples
c#exceptionradixcustom-exceptions

Custom exceptions and base constructor


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?


Solution

  • 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.