c#异常处理

来源:互联网 发布:开淘宝的经验分享 编辑:程序博客网 时间:2024/05/22 13:16

模拟银行应用系统中的存钱和取钱功能,定义一个异常类完成当用户取钱额大于存折余额时进行异常处理,并给用户一个友好的提示。

这里写图片描述

copyright vivi_and_qiao liweiusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication3{    class withdrawexcept : Exception    {        public void show()        {            Console.WriteLine("您的账户余额没有这么多钱!程序已经终止");        }    }    class Account    {        double money;        public Account(double m)        {            money = m;        }        public void setmoney(double m)        {            money = m;        }        public void withdraw()        {            double withdraw;            Console.WriteLine("请输入取款金额!");            withdraw = double.Parse(Console.ReadLine());            if (withdraw > money)                throw new withdrawexcept();            else                money -= withdraw;        }    }    class Program    {        static void Main(string[] args)        {            Account account=new Account(100);            try            {                account.withdraw();            }            catch(withdrawexcept e)            {                e.show();            }            Console.ReadKey();        }    }}
0 0
原创粉丝点击