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?
Your
Exceptionsclass needs to define all constructors you want to provide. The constructors ofSystem.Exceptionare not virtual or abstract. The keywordbasedoes 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: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:
You might also take a look at this page.