📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace LT14_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "捕获异常应用示例";
label1.Text = "输入被除数";
label2.Text = "输入除数";
label3.Text = "";
button1.Text = "计 算";
button2.Text = "退 出";
}
private void button1_Click(object sender, EventArgs e)
{
int a, b , c;
try //试图捕获异常
{
//throw new DivideByZeroException();
a = Convert.ToInt32(textBox1.Text); //将文本转换为Int32类型的整数
b = Convert.ToInt32 (textBox2.Text);
c = a / b;
label3.Text = "两数的商为:"+c.ToString();
}
catch (FormatException) //处理转换发生的异常
{
label3 .Text = "请将被除数或除数的值输入为数字!";
}
catch (DivideByZeroException) //处理除数为零的异常
{
label3 .Text = "除数不能为零!";
}
finally // 清除异常
{
MessageBox.Show("感谢使用本软件,程序正常结束","finally");
}
}
private void button2_Click(object sender, EventArgs e)
{
throw new DivideByZeroException("除数不能为0!");
//this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -