本文介绍了创建我自己的异常C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循我的C#书籍中的示例,我遇到了在Visual Studio中不起作用的书籍示例。它涉及创建自己的异常,特别是要阻止您获取负数的平方根。但是,当我使用 throw new创建NegativeNumberException时,出现错误,提示找不到类型或名称空间名称'NegativeNumberException'(您是否缺少using指令或程序集引用?)

Following examples in my C# book and I came across a book example that doesn't work in Visual Studio. It deals with creating your own exceptions, this one in particular is to stop you from taking the square root of a negative number. But when I create the NegativeNumberException by using "throw new" I get an error that says "The type or namespace name 'NegativeNumberException' could not be found (are you missing a using directive or an assembly reference?)"

如果这不是正确的方法,如何创建自己的异常?也许我的书已经过时了?代码如下:

How can I create my own exceptions if this isn't the right way? Maybe my book is outdated? Here's the code:

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 只是一个。恕我直言,有两个棘手的事情与用户定义的异常:

Exception is just a class like many other classes in .Net. There're, IMHO, two tricky things with user defined exceptions:


  1. Exception 继承您的异常类,并非来自过时的 ApplicationException

  2. 在典型情况下,用户定义的异常应具有许多构造函数- 4

  1. Inherit your exception class from Exception, not from obsolete ApplicationException
  2. User defined exception should have many constructors - 4 in the typical case

类似的东西:

public class NegativeNumberException: Exception {
  /// <summary>
  /// Just create the exception
  /// </summary>
  public NegativeNumberException()
    : base() {
  }

  /// <summary>
  /// Create the exception with description
  /// </summary>
  /// <param name="message">Exception description</param>
  public NegativeNumberException(String message)
    : base(message) {
  }

  /// <summary>
  /// Create the exception with description and inner cause
  /// </summary>
  /// <param name="message">Exception description</param>
  /// <param name="innerException">Exception inner cause</param>
  public NegativeNumberException(String message, Exception innerException)
    : base(message, innerException) {
  }

  /// <summary>
  /// 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
  /// </summary>
  /// <param name="info">Serialization info</param>
  /// <param name="context">Serialization context</param>
  protected NegativeNumberException(SerializationInfo info, StreamingContext context)
    : base(info, context) {
  }
}

这篇关于创建我自己的异常C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 03:34