3

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?

4

4 回答 4

6

您的Exceptions类需要定义您要提供的所有构造函数。的构造函数System.Exception不是虚拟的或抽象的。该关键字base不会调用所有基类的成员,而是调用您在类声明中提供的一个基类的成员。看看这个:

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

默认情况下定义无参数构造函数。要隐藏它,您需要声明它private

关于MSDN,您应该保持异常继承层次结构平坦:

如果您正在设计需要创建自己的异常的应用程序,建议您从 Exception 类派生自定义异常。最初认为自定义异常应该从 ApplicationException 类派生;然而,在实践中,这并没有增加显着的价值。

你也可以看看这个页面

于 2013-02-11T08:29:59.597 回答
1

完全删除Exceptions该类并让ProccessIsNotStarted直接继承自System.Exception.

类的构造函数不会自动“复制”到派生类;它们可以使用base,但您必须手动定义它们。

于 2013-02-11T08:26:44.213 回答
1

base 指的是直接基类,而不是链下的任何基类。您的 ProcessIsNotStarted 类是 RegisService.Exceptions 的直接子类型,而不是 System.Exception。RegisService.Exceptions 没有带有签名 (string, Exception) 或 (string) 的构造函数。

尝试将这两个构造函数添加到您的 RegisService.Exceptions 基类。

于 2013-02-11T08:26:46.990 回答
1

如果您检查以下代码:

public class Exceptions : Exception
{        
}

你会注意到没有构造函数。嗯,这是一个谎言,因为可以使用默认的公共构造函数,但是没有自定义的构造函数。

如果你想公开Exceptionthrough的构造函数,Exceptions那么你将不得不定义它们Exceptions并从那里调用它们base,因为继承异常调用base正在调用Exceptions,因此Exception不是它们base,因此构造函数不可访问。

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

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

然后,你可以做得new Exceptions("", null)很好。而且,您的基本构造函数在使用继承时调用。

我不知道你是否从这个继承链中获得了任何价值,你可能想按照另一个建议去掉中间人,可以这么说。

于 2013-02-11T08:27:22.713 回答