如何修复 C# 中 wpf 项目的 "Type caught or thrown must be derived from system.exception" 错误?

How to fix "Type caught or thrown must be derived from system.exception" error for a wpf project in C#?

我正在尝试为用户输入创建验证策略。但是我一直收到 CS0155 错误。

我试过抛出异常,但并没有消除错误。

    catch (OverflowAction)
            {
                Debug.WriteLine(
                    "{0}.Validate: Int32 overflow (\"{1}\").",
                    GetType(), str);

                string errmsg = Properties.Resources.OverflowError;
                return new ValidationResult(false, errmsg);
               //throw new NotImplementedException();
            }

我希望验证器捕获异常并return一条错误消息。

此错误表明您的 OverflowAction class 没有继承自 Exception(或派生的)。

请参阅 CS0155 错误文档。

Only data types that derive from System.Exception can be passed into a catch block.

OverflowAction 应该像

class OverflowAction : Exception
{
    // ...
}

您可能会混淆 OverflowActionOverflowException ...