如何正确处理此异常?

时间:2012-10-05 14:51:24

标签: c# .net exception user-interface dll

我仍然是一名新手程序员(来自C ++),我正在创建一个GIU,它显示正在运行的软件中的Tag。目的是显示标记属性DescriptionEng Units。我有2个dll InTouchDataAccess和NDde。 我读过关于异常处理的事情,我见过的最好的想法就是这个: Create a Validate function

我做了。但程序没有进入函数,我直接进入SelectButton_Click fct的catch块。

TagBrowser.cs

using System;
using System.Windows.Forms;
using IOM.InTouchDataAccess;

namespace InTouchTagBrowser
{
    public partial class InTouchTagBrowser : Form
    {
        public string tagName;
        public string description;
        public string engUnits;

        public InTouchTagBrowser()
        {
            InitializeComponent();
        }

        private void TagBrowser_Load(object sender, EventArgs e)
        {
        }

        private void SelectButton_Click(object sender, EventArgs e)
        {
            try
            {
                tagName = tagNameBox.Text;
                InTouchDdeWrapper inTouchWrapper = new InTouchDdeWrapper();
                inTouchWrapper.Initialize();

                TagDotField tagDotField = new TagDotField(tagName);

                string value = inTouchWrapper.Read(tagName);

                if (EngValidate(inTouchWrapper.Read(tagDotField.EngUnits)) != 0)
                {
                    engUnits = inTouchWrapper.Read(tagDotField.EngUnits);
                }

                else
                {
                    engUnits = "N/A";
                }

                if (inTouchWrapper.Read(tagDotField.Description) != "")
                {
                    description = inTouchWrapper.Read(tagDotField.Description);
                }
                else
                {
                    description = "N/A";
                }

                descriptionlbl.Text = description;
                englbl.Text = engUnits;
                valuelbl.Text = value;
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show(ex.Source);
                MessageBox.Show(ex.HelpLink);
                MessageBox.Show(ex.StackTrace);
            }
        }

        private void WriteButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (tagName == "")
                {
                    MessageBox.Show("Please enter a tag!");
                }
                else
                {
                    string inputValue = ValueBox.Text;
                    InTouchDdeWrapper inTouchWrapperWriter = new InTouchDdeWrapper();
                    inTouchWrapperWriter.Initialize();

                    TagDotField tagWriter = new TagDotField(inputValue);
                    inTouchWrapperWriter.Write(tagName, inputValue);
                    valuelbl.Text = inputValue;
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                MessageBox.Show("Tag change successfull");
            }
        }

        public int EngValidate(string engString)
        {
            string exception;
            int x;

            try
            {
                InTouchDdeWrapper inTouchWrapper = new InTouchDdeWrapper();
                inTouchWrapper.Initialize();
                TagDotField tagDotField = new TagDotField(tagName);
                engString = inTouchWrapper.Read(tagDotField.EngUnits);

                x = 1;
            }
            catch (Exception msg)
            {
                exception = msg.ToString();
                if (exception == "")
                    x = 1;
                else
                    x = 0;
            }

            return x;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您为自己实际未使用的方法声明变量engString。此外,您设置此变量的方式从包装器读取,这可能导致异常(然后由外部catch块捕获)。更好的方法是声明这样的方法只读一次:

public bool TryRead(InTouchDdeWrapper wrapper, string prop, out string value) 
{
  try 
  {
    value = wrapper.Read(prop);
    return true;
  }
  catch (Exception e)
  {
    value = null;
    return false;
  }  
}

这样称呼:

InTouchDdeWrapper wrapper = new InTouchDdeWrapper();
wrapper.Initialize();

TagDotField field = new TagDotField(tagName);

string engUnits;

if (!TryRead(wrapper, field.EngUnits, out engUnits)) 
{
  engUnits = "N/A";
}

我希望你能理解这种模式。你在代码中反复实例化(并初始化)很多类,最好只有一个包装器,一个字段等......并且只读取一次值。另外,你不应该在C#中使用返回码,这就是例外情况。

答案 1 :(得分:0)

我建议你调整global treatment for exception

您需要处理Windows窗体的System.Windows.Forms.Application.ThreadException event

链接:http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx

Nota:您删除了表单中所有已实现的try-catch,并且只设置了一个处理