创建我自己的exceptionc#

以下是我的C#书中的示例,我遇到了一个在Visual Studio中不起作用的书籍示例。 它涉及创建自己的exception,尤其是阻止你取负数的平方根。 但是当我使用“throw new”创建NegativeNumberException时,我收到一条错误,上面写着“找不到类型或命名空间名称’NegativeNumberException’(您是否缺少using指令或程序集引用?)”

如果这不是正确的方法,我怎么能创建自己的例外? 也许我的书已经过时了? 这是代码:

class SquareRootTest { static void Main(string[] args) { bool continueLoop = true; do { try { Console.Write("Enter a value to calculate the sqrt of: "); double inputValue = Convert.ToDouble(Console.ReadLine()); double result = SquareRoot(inputValue); Console.WriteLine("The sqrt of {0} is {1:F6)\n", inputValue, result); continueLoop = false; } catch (FormatException formatException) { Console.WriteLine("\n" + formatException.Message); Console.WriteLine("Enter a double value, doofus.\n"); } catch (NegativeNumberException negativeNumberException) { Console.WriteLine("\n" + negativeNumberException.Message); Console.WriteLine("Enter a non-negative value, doofus.\n"); } } while (continueLoop); }//end main public static double SquareRoot(double value) { if (value < 0) throw new NegativeNumberException( "Square root of negative number not permitted."); else return Math.Sqrt(value); } } 

Exception只是像.Net中的许多其他类一样的类。 恕我直言,有两个棘手的事情,用户定义的例外:

  1. Exceptioninheritance您的exception类,而不是从过时的ApplicationExceptioninheritance
  2. 用户定义的exception应该有许多构造函数 – 在典型情况下为4

像这样的东西:

 public class NegativeNumberException: Exception { ///  /// Just create the exception ///  public NegativeNumberException() : base() { } ///  /// Create the exception with description ///  /// Exception description public NegativeNumberException(String message) : base(message) { } ///  /// Create the exception with description and inner cause ///  /// Exception description /// Exception inner cause public NegativeNumberException(String message, Exception innerException) : base(message, innerException) { } ///  /// Create the exception from serialized data. /// Usual scenario is when exception is occured somewhere on the remote workstation /// and we have to re-create/re-throw the exception on the local machine ///  /// Serialization info /// Serialization context protected NegativeNumberException(SerializationInfo info, StreamingContext context) : base(info, context) { } } 

所有exception都是类似于其他exception的类型,如果要定义自定义类型的exception,您实际上必须为它创建一个类:

 /* You could also use ArgumentException, etc. */ class NegativeNumberException : Exception { … } … 

由于.NET 3.0自定义exception应该从Exception而不是ApplicationException 派生 ,所以使用这个:

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

您需要声明自定义Exception NegativeNumberException类。

所有自定义exception都是Exception的子类,因此从Exception类派生NegativeNumberException

来自MSDN:

如果您正在设计需要创建自己的exception的应用程序,建议您从Exception类派生自定义exception。 最初认为自定义exception应该来自ApplicationException类; 然而,在实践中,尚未发现这增加了重要价值。 有关更多信息,请参阅处理exception的最佳实践。 ApplicationException使用HRESULT COR_E_APPLICATION,其值为0x80131600。

试试这个:

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