【问题标题】:C# Assign Name for Try-Catch ErrorC# 为 Try-Catch 错误分配名称
【发布时间】:2011-07-17 23:35:15
【问题描述】:

每个人都在 C# 中使用Try-Catch。我知道。

例如;

static void Main()
    {
        string s = null; // For demonstration purposes.

        try
        {            
            ProcessString(s);
        }

        catch (Exception e)
        {
            Console.WriteLine("{0} Exception caught.", e);
        }
    }
}

一切正常。

但是如何为特定错误指定名称?

例如;

try
{
   enter username;
   enter E-Mail;
}
catch
{

}
  • 如果用户名已经存在,ErrorMessage-->(“用户名已经存在”)
  • 如果电子邮件已经存在,ErrorMessage -->(“电子邮件正在使用”)

我如何在C# 中做到这一点?

最好的问候,

发声

【问题讨论】:

  • 检查名称是否已存在不应引发异常 IMO。
  • 这个注释是否意味着我们可以放心地忽略这个问题!
  • @Brian:如果在检查过程中出现问题,可能会发生这种情况,但是,这不是重点:P

标签: c# error-handling try-catch


【解决方案1】:
try
{
    if (username exists)
    {
        throw new Exception("The Username Already Exist");
    }

    if (e-mail exists)
    {
        throw new Exception("The E-Mail Already Exist");
    }
}
catch(Exception ex)
{
    Console.WriteLine("The Error is{0}", ex.Message);
}

【讨论】:

    【解决方案2】:

    在 C# 中,可以创建自己的异常类。但是 Exception 必须是 C# 中所有异常的最终基类。因此,用户定义的异常类必须继承自 Exception 类或其标准派生类之一。

    using System;
    class MyException : Exception
    {
      public MyException(string str)
      {
        Console.WriteLine("User defined exception");
      }
    }
    class MyClass
    {
      public static void Main()
      {
        try
        {
          throw new MyException("user exception");
        }
        catch(Exception e)
        {
          Console.WriteLine("Exception caught here" + e.ToString());
        }      
      }
    }
    

    您可以在应用程序的任何位置抛出这些异常并捕获它们,尽管在这种情况下我不会使用异常。

    【讨论】:

      【解决方案3】:

      我认为所有这些答案都对这个问题有意义,但如果您在未来某个时候想要对每个异常进行不同的处理,您可以按如下方式进行:

      下一个示例假设您实现了两个不同的异常

      try
      {
          if user name is exit
          {
              throw new UserNameExistsException("The Username Already Exist");
          }
      
          if e-mail is already exit
          {
              throw new EmailExistsException("The E-Mail Already Exist");
          }
      }
      catch(UserNameExistsException ex)
      {
          //Username specific handling
      }
      catch(EmailExistsException ex)
      {
          //EMail specific handling
      }
      catch(Exception ex)
      {
          //All other exceptions come here!
          Console.WriteLine("The Error is{0}", ex.Message);
      }
      

      【讨论】:

      • +1 表示没有错过重点。 OP 的问题本身并不完全清楚.. 他是否需要自定义错误消息或每个 错误类型 的自定义捕获块
      【解决方案4】:

      你可以这样做:

      if (UsernameAlreadyExcists(username))
      {
          throw new Exception("The Username Already Exist");
      }
      

      【讨论】:

        【解决方案5】:
        if(UserNameAlreadyExists(username))
        {
           throw new Exception("Username already exists");
        }
        if(EmailAlreadyExists(email))
        {
           throw new Exception("Email already exists");
        }
        

        这将回答您的问题。

        但异常处理不能用于执行这些检查。异常代价高昂,适用于无法从错误中恢复的异常情况。

        【讨论】:

          【解决方案6】:

          当您抛出异常时,您可以为它们分配消息:

          throw new Exception("The username already exists");
          

          但我认为您不应该在这里抛出异常,因为您的应用程序会预期导致这些错误的输入;它们不是异常条件。也许您应该使用验证器或其他类型的处理程序。

          【讨论】:

          • 如何通过 ASP.NET 验证器使用这个“抛出新的..”?
          猜你喜欢
          • 2018-08-05
          • 1970-01-01
          • 2013-08-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-22
          • 1970-01-01
          • 2014-01-26
          相关资源
          最近更新 更多