首页 新闻 会员 周边

异常处理问题

0
[待解决问题]

class YC:FormatException
    {
        private string Message;
        public YC()
        {

            this.Message = "输入有误,请重新输入";
        }

        public override string  ToString()
       {
          return Message ;
         }
   
    }
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
         
                string a = textBox1.Text;
             try
             {
                int b = int.Parse(textBox2.Text);

                throw new YC();
             }
            catch (YC e)
             {
                MessageBox .Show (e .ToString ());
             }
            finally
             {
            
            
                bool bl = ceshi(a, b);

                if (bl)
                {
                    MessageBox.Show("CHENGONG");
                }
                else
                {
                    MessageBox.Show("SH");
                }
            }
           
        }
        
        }

我设置了断点,在textbox2中输入了汉字之后,程序停在YC构造方法后面提示书字符串格式不正确,按F11无法向下执行,我预想的情形是弹出对话框,请高手帮忙

870087629的主页 870087629 | 初学一级 | 园豆:170
提问于:2010-09-16 10:31
< >
分享
所有回答(2)
0

有些不太清楚,想了解一下解执行步骤是不是这样的:

1. string a = textBox1.Text;
2.            try
3.             {
4.               int b = int.Parse(textBox2.Text);

                throw new YC();
5.             }
6.           catch (YC e)
7.             {
8.                MessageBox .Show (e .ToString ());
9.            }

 

在执行第4步时出错.

接下来会先执行throw new YC(); 还是先跳到catch里面??

changbluesky | 园豆:854 (小虾三级) | 2010-09-16 23:50
0

 int b = int.Parse(textBox2.Text);

会自己抛出  Format异常

 

接这个异常才可

参考  tryParse 

xuld | 园豆:494 (菜鸟二级) | 2010-09-19 00:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册