Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自定义异常_C#_.net_C# 4.0_Exception Handling - Fatal编程技术网

C# 自定义异常

C# 自定义异常,c#,.net,c#-4.0,exception-handling,C#,.net,C# 4.0,Exception Handling,我们有一个自定义的异常类,它在我们的终端上运行良好(参见下面的代码1)。如果传入的参数isverify为true,我需要在这句话中附加一条错误消息(“更改尚未提交”)。我对这个类做了一些修改(参见代码2),但在抛出错误时,我似乎仍然得到了原始消息。我真的很感谢你的帮助。非常感谢 代码1: public class BusinessRuleValidationException : Exception { public BusinessRuleValidationException(str

我们有一个自定义的异常类,它在我们的终端上运行良好(参见下面的代码1)。如果传入的参数isverify为true,我需要在这句话中附加一条错误消息(“更改尚未提交”)。我对这个类做了一些修改(参见代码2),但在抛出错误时,我似乎仍然得到了原始消息。我真的很感谢你的帮助。非常感谢

代码1:

public class BusinessRuleValidationException : Exception
{
    public BusinessRuleValidationException(string message):base(message)
    {
    }
}
代码2:

   public BusinessRuleValidationException(string message, bool isVerified)
        : base(message)
    {
        if (isVerified)
            message += " The change has not been committed.";
    }

你可以试试这个,但我还没有测试过:

public BusinessRuleValidationException(string message, bool isVerified)
    : base(isVerified ? (message += " The change has not been committed.") : message) 
{ }

问题是在修改消息之前调用了
异常
基类的构造函数:

public BusinessRuleValidationException(string message, bool isVerified)
    : base(message) // <- problem is here
{
    if (isVerified)
        message += " The change has not been committed.";
}

下面是关于为什么属于@JasonLarke的解释,谢谢你的解释。我想把你的评论也标记为答案不幸的是,我们只能标记一个答案。我对你的评论投了赞成票。谢谢。
public BusinessRuleValidationException(string message, bool isVerified)
    : base(string.Format("{0}{1}", message, isVerified ? " The change has not been committed." : string.Empty))
{ }