【问题标题】:try-catch exception. For some reason the program won't catch any errors尝试捕获异常。由于某种原因,程序不会捕获任何错误
【发布时间】:2019-03-27 17:27:52
【问题描述】:

我已经尝试了多种方法让我的程序通过使用 try get 来捕获错误,当用户没有在其中一个文本框的文本框中输入任何数据时,当用户没有准确输入 9 时数字到文本块。我正在使用 C# WPF。

我尝试了很多不同的方法。一个似乎有效的方法是当我转换为整数时,它似乎由于某种原因捕获了它,但我使用的是字符串。例如

try
{
    // remmeber, textboxes always capture the data as a string therefore we need to convert to an integer
    CourseDetails.Name = Convert.ToInt32(txtName.Text);
    CourseDetails.SCNnumber = Convert.ToInt32(txtSCNnumber.Text);
}

// if something does go wrong with any of the instructions in the try block then we will catch the error rather than crash the program
catch (Exception)
{
    MessageBox.Show("Please complete all fields");
    return;
}

try
{
    if (txtName.Text.Length < 0)
    {
        MessageBox.Show("Enter your full name");
    }

    else
    {
        CourseDetails.Name = txtName.Text;
    }

    if (txtSCNnumber.Text.Length != 9)
    {
        MessageBox.Show("SCN number must be 9 characters long");
    }
    else
    {
        CourseDetails.SCNnumber = txtSCNnumber.Text;

    }
}
catch (Exception)
{
    MessageBox.Show("Please complete all fields");
}

我正在寻找的结果是,当用户在第一个文本框中输入数据时,应将其保存到变量 CourseDetails.Name 中,否则如果将其留空,程序会将其视为错误并显示信息。 对于第二个文本框,如果用户输入的不是 9 个字符,那么程序将显示一条错误消息,指出电话号码必须超过 9 个字符。否则程序会将用户输入保存到变量CourseDetails.SCNnumber

【问题讨论】:

  • 注意:您可能需要考虑string.IsNullOrWhitespace(txtName.Text) 来检查是否输入了名称。不允许只输入空格。

标签: c# wpf try-catch


【解决方案1】:

try-catch 块捕获异常。要捕获异常,必须抛出异常。您的第一个 try-catch 块将起作用,因为如果输入无效,Convert.ToInt32 将抛出 FormatException,如记录的 here 所述。

要使第二个 try-catch 块起作用,您必须对无效输入抛出异常。

try
{
    if (txtName.Text.Length < 0)
    {
        throw new ValidationException("Please enter user name")
    }
    // ...
}
catch(ValidationException ex)
{
    MessageBox.Show(ex.Message);
}

如您所见,我发现了一种特定的异常类型。捕捉Exception 类型通常是不好的做法,因为您可能会捕捉到在该catch 块内无法正确处理的异常。吞下这些会显着增加调试难度。

我还要注意异常不是执行更复杂的验证逻辑的完美方式,因为throw 会直接跳转到下一个匹配的捕获,因此并非所有字段都会被验证。

【讨论】:

    【解决方案2】:

    您必须了解 Try-Catch 块的用途。它们的主要作用是处理程序中的异常。这些异常可以是 CLR 抛出的编译器异常,也可以是程序出错时的程序代码。需要处理这些异常以防止程序崩溃。 C# 提供内置支持来使用 try、catch 和 finally 块处理异常。

    现在在您的代码中,不要在您的 Exception 块中显示您的 MessageBox.Show。这基本上意味着只有当抛出异常时,您的 MessageBox 才会显示。如果您的txtName.Text 的整数转换不正确,则会出现此异常(参考您的代码)。

    改为在您的场景中使用 If-Else 条件。例如:

    //Try to parse your captured data to Integer
    try
    {
    
        if(txtName.Text == "" && txtSCNnumber.Text == "")
        {
         MessageBox.Show("Please complete all fields");
        }
        else
        {
        // remmeber, textboxes always capture the data as a string therefore we need to convert to an integer
         CourseDetails.Name = Convert.ToInt32(txtName.Text);
         CourseDetails.SCNnumber = Convert.ToInt32(txtSCNnumber.Text);
        }
    }
    //If the parse fails, then throw an exception
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    

    【讨论】:

      【解决方案3】:

      感谢您的意见。在阅读了您的 cmets 后,我决定放弃 Try-Catch,我意识到它不适合我想做的事情。我使用 If-Else 语句保持简单,如下所示。

          if (txtName.Text == "" && txtSCNnumber.Text == "") 
      
              {
                  MessageBox.Show("Please complete all fields");   
      
                  txtName.Focus();  
      
              }
              else if (txtSCNnumber.Text.Length != 9)
              {
                  MessageBox.Show("You have entered an invalid SCN number"); 
                  txtSCNnumber.Focus(); 
              }
              else
              {
      
                  CourseDetails.Name = txtName.Text;  
                  CourseDetails.SCNnumber = txtSCNnumber.Text; 
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-04
        • 1970-01-01
        • 2014-06-19
        • 1970-01-01
        • 1970-01-01
        • 2014-05-11
        • 2022-06-21
        相关资源
        最近更新 更多