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无法向下执行,我预想的情形是弹出对话框,请高手帮忙
有些不太清楚,想了解一下解执行步骤是不是这样的:
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里面??
int b = int.Parse(textBox2.Text);
会自己抛出 Format异常
接这个异常才可
参考 tryParse