C# 实验六

1. 委托事件练习:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    delegate void MyDelegate();
    class D
    {
        public event MyDelegate myEvent;
        private int score;
        public int Score
        {
            set{score = value;}
            get { return score; }
        }
        public void Output()
        {
            Console.WriteLine("Score=" + Score);
        }
        public void SetEvent()
        {
            myEvent();
        }
        public D(int a)
        {
            Score = a; // 为score赋值
        }
    }
    class Program
    {
        
        public static void Fun()
        {
            Console.WriteLine("运行了Fun方法");
        }
        public static void Main(string[] args)
        {
            Console.WriteLine("请设置score值(整数):");
            int _score = int.Parse(Console.ReadLine());
            D d1 = new D(_score);
            D d2 = new D(_score);

            d1.myEvent += Fun;
            d2.myEvent += d2.Output;

            d1.SetEvent();
            d2.SetEvent();
            

            Console.ReadKey(true);  // 停顿作用
        }
    }
}

运行截图

在这里插入图片描述

2. 异常处理练习:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 异常处理
{
    class Program
    {
        static void Main(string[] args)
        {
            double a, b,c;
            Console.WriteLine("请分别输入a,b的值:");
            try
            {
                a = double.Parse(Console.ReadLine());

                try
                {
                    b = double.Parse(Console.ReadLine());
                    if (b == 0.0) throw new DivideByZeroException("除数不能为0!");
                }
                catch (DivideByZeroException e)
                {
                    throw e;
                }
                c = a / b;
            }catch(FormatException fe){
                Console.WriteLine(fe.Message);
                Console.WriteLine(fe.StackTrace);      
            }

        }
    }
}

运行截图

在这里插入图片描述
在这里插入图片描述

3. 异常处理练习_图形界面:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 异常处理__窗体程序
{
    public partial class Form1 : Form
    {
        double a, b,c;
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                a = double.Parse(textBox1.Text);
            }
            catch(Exception)
            {
                MessageBox.Show("输入的数据不符合规范!");
                textBox1.Text = "";
            }

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string b_str = textBox2.Text;
                b = double.Parse(b_str);
                if (b == 0.0) throw new DivideByZeroException();    // 手动抛出异常
            }
            catch (FormatException)
            {
                // 测试代码
                // MessageBox.Show(ex.GetType().ToString());
                MessageBox.Show("输入的数据不符合规范!");
                textBox2.Text = "";
            }
            catch (DivideByZeroException)
            {
                MessageBox.Show("除数不能为0!");
                textBox2.Text = "";
            }

        }

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            c = a / b;
            textBox3.Text = c.ToString();


        }
    }
}

运行截图

当输入数据为字符是,捕捉FormatExcetion异常:
在这里插入图片描述
当除数为0时,捕捉除零异常:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值