您的位置:首页 > 其它

丶使用 checked 关键字处理 溢出 错误信息

2011-10-17 01:25 176 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Example22
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
byte bt_One, bt_Two; //定义两个 byte 变量
if (byte.TryParse(textBox1.Text, out bt_One) && byte.TryParse(textBox2.Text, out bt_Two)) //为两个 byte变量赋值
{
try
{
checked { bt_One += bt_Two; } //使用 checked 关键字判断是否溢出
textBox3.Text = bt_One.ToString(); //输出 相加后的结果
}
catch (OverflowException ex)
{
MessageBox.Show(ex.Message, "出错!");
}
}
else
{
MessageBox.Show("请输入 255 以内的数字!");
}
}

private void Form1_Shown(object sender, EventArgs e)
{
this.Text = "使用 checked 关键字处理 溢出 错误信息";
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐