【问题标题】:How to create your own exception如何创建自己的异常
【发布时间】:2013-07-15 13:23:14
【问题描述】:

我专门寻找控制台应用程序的解决方案,但也欢迎表单应用程序的答案。 您能帮我解决以下问题吗?

我的问题: 我想创建自己的异常,当用户键入其中一个时,它将捕获从 5 到 9 的任何数字。

注意:我知道我可以通过简单地使用 IF ELSE 语句来解决这个问题,但我特别希望将它作为异常捕获。

我不明白的地方: 例如,一旦用户输入 5,我自己创建的异常就会捕获它 - 我不明白的是如何告诉我创建的异常类要捕获什么,要查找什么?我在哪里可以在我的 Exception 中输入这些数字并告诉我的 Exception 类这些数字是例外?

如果我不够清楚,请告诉我,我会尝试改写自己。

【问题讨论】:

  • 异常不会主动寻找违规行为。您可能正在寻找某种形式的验证框架。
  • 你能展示一小段代码吗?
  • 我认为您误解了异常的用途。异常应该表明出现问题,这就是它使您的程序崩溃的原因!无论如何,您都需要if / else
  • 是的,您需要验证。例外情况不同
  • 我认为您以完全无效的方式使用名称 Exception。异常被抛出被捕获。他们没有捕捉任何东西。 Exception 基本上是一个简单的数据持有者对象,它详细解释了抛出它的原因。

标签: c# exception try-catch


【解决方案1】:

您可能会从异常 tutorial 中受益。

听起来你正在尝试做三件事。

1 从文本输入字段中读取一个数字。

2 判断是否为有效数字。

3 如果数字无效,则抛出异常。

//Read input
int i = -1;
i = int.TryParse(MyTextField.Text, out i);

if (i >= 5 && i <= 9) 
    throw new ArgumentOutOfRangeException("value", "Value cannot be  between 5 - 9.");

【讨论】:

    【解决方案2】:

    异常不只是发生。当您的代码抛出异常时,例如,如果您尝试除以 0 并得到 DivideByZeroException - 这意味着某处的某些代码必须有类似 throw new DivideByZeroException(); 的行。 (诚​​然,这是对事情的简化。)

    所以为了抛出你想要的异常 - 你必须测试输入,如果它不好然后抛出一个异常。

    假设你不是说你需要一个自定义异常(例如FiveToNineException)——你可以使用这个:

    if (i >= 5 && i <= 9)
    {
        throw new Exception("5 to 9 Exception");
    }
    

    或者:

    if (i >= 5 && i <= 9)
    {
        Exception e = new Exception("5 to 9 Exception");
        e.Data.Add("The number ", i);
        throw e;
    }
    

    编辑

    对于一个非常简单的自定义异常:

    public class FiveToNineException : System.Exception
    {
        public FiveToNineException() : base() { }
    }
    

    然后你可以拥有:

    throw new FiveToNineException();
    

    还有:

    try {/*Do something*/ }
    catch (FiveToNineException ex) { }
    

    有关更多信息,请参阅 this link 以获取我的问题的答案。

    【讨论】:

    • 嗯,这有点像我在寻找创建我的自定义异常 :) 为示例起见,将其命名为“FiveToNineException”,以便我可以将它与 CATCH (FiveToNineException) 一起使用。有什么办法吗?或链接到如何做到这一点?感谢您关注我的问题。
    【解决方案3】:

    如果我理解你是正确的,我认为你应该尝试类似:

    if (yourNumber >= 5 && yourNumber <= 9)
    {
        throw new YourException(..);
    }
    

    但也请参阅 cmets。您对异常的理解不正确。

    【讨论】:

      【解决方案4】:

      以下代码显示了 CustomException 的基本示例

      class Program
      {
          static void Main(string[] args)
          {
              try
              {
                  int x = Convert.ToInt32(Console.ReadLine());
      
                  if (x >= 5 && x <= 9)
                  {
                      CustomException e = new CustomException("Please Eneter Another Number");
                      throw e;
                  }
              }
      
              catch (CustomException ex)
              {
                  Console.WriteLine(ex.Message);
              }
          }
      }
      
      public class CustomException : System.Exception
      {
          public CustomException(string txt)
              : base(txt)
          {
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-04-29
        • 2013-12-21
        • 1970-01-01
        • 1970-01-01
        • 2015-11-29
        • 1970-01-01
        • 2014-03-31
        • 2011-10-05
        • 1970-01-01
        相关资源
        最近更新 更多