【问题标题】:c# Custom Exceptionsc# 自定义异常
【发布时间】:2019-03-06 14:42:22
【问题描述】:

我无法让我的代码(如下)执行异常!

  1. 首先,银行账户是空的。
  2. 存入1500元
  3. 尝试提现1050但透支限额为1000
  4. 由于提款金额大于透支限额 OverdraftEx 异常应该显示我的消息(但不是)。
  5. 然后尝试提取 1000,这应该是成功的,并且 余额应该变成 500
  6. 现在,尝试提款 50 但应显示第二条异常消息 DailyLimitEx 超出每日限额,因为每日提款限额为 1000 并且当天已经提款,再次没有显示此异常。
  7. 然后,将显示将 DailyLimit 重置回 0 的 End of the Day。
  8. 然后允许尝试提取 50,因为这是新的一天。

我无法显示异常消息。 你能帮忙吗?最后有一个示例输出。

谢谢


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

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Account account = new Account();
            try
            {
                double overdraftLimit = 1000;
                double dailyWithdrawlLimit = 1000;
            }
            catch (OverdraftEx e1)
            {
                Console.WriteLine(e1.Message);
            }
            catch (DailyLimitEx e2)
            {
                Console.WriteLine(e2.Message);
            }

            account.display();
            account.deposit(1500);
            account.withdraw(1050);
            account.withdraw(1000);
            Console.WriteLine("-------------------");
            account.withdraw(50);
            account.endDay();
            account.withdraw(50);

            Console.ReadLine();
            return;
        }
    }

    class OverdraftEx : Exception
    {
        private static string msg = " Sorry, the amount you are trying to withdraw exceeds your allowed overdraft limit.";
        public OverdraftEx() : base(msg)
        {
        }
    }

    class DailyLimitEx : Exception
    {
        private static string msg1 = " Sorry, you have exceeded your allowed daily withdrawal limit.";
        public DailyLimitEx() : base(msg1)
        {
        }
    }

    class Account
    {
        public int accountID { get; set; }
        public double amount;
        public double balance;
        public double Balance
        {
            get
            {
                return balance;
            }
            set
            {
                if (amount > overdraftLimit)
                {
                    OverdraftEx ex1 = new OverdraftEx();
                    throw ex1;
                }
            }
        }

        public double overdraftLimit { get; set; }

        public double dailyWithdrawlLimit
        {
            get
            {
                return dailyWithdrawlLimit;
            }
            set
            {
                if (amount > dailyWithdrawlLimit - 1000)
                {
                    DailyLimitEx ex2 = new DailyLimitEx();
                    throw ex2;
                }
            }
        }

        public Account()
        {
            accountID = 10;
            balance = 0;
            return;
        }

        public Account(int accID, double accBalance)
        {
            accountID = accID;
            balance = accBalance;
            return;
        }

        public void withdraw(double amount)
        {
            Console.WriteLine("");
            Console.WriteLine(" Attempting to withdraw $" + Math.Round(amount, 2));
            balance = balance - amount;

            Console.WriteLine(" Account #: " + accountID);
            Console.WriteLine(" Your new balance is $" + Math.Round(balance, 2));
        }

        public void deposit(double amount)
        {
            // calculating the balance after depositing money

            if (amount > 0)
            {
                Console.WriteLine(" Depositing $" + Math.Round(amount, 2));
                balance = balance + amount;
                Console.WriteLine(" Account #: " + accountID);
                Console.WriteLine(" Balance: $" + Math.Round(balance, 2));
            }
            else
                Console.WriteLine(" Sorry, amount is invalid");

            Console.WriteLine("");
            return;
        }

        public void display()
        {
            Console.WriteLine("");
            Console.WriteLine(" Account ID: " + accountID);
            Console.WriteLine(" Balance: $" + Math.Round(balance, 2));
            Console.WriteLine("");

            return;
        }
        public void endDay()
        {
            Console.WriteLine("");
            Console.WriteLine(" End Day");
            Console.WriteLine(" Account ID: " + accountID);
            Console.WriteLine(" Balance: $" + Math.Round(balance, 2));
            return;
        }
    }
}

示例输出: 注意:示例输出中的第一条错误消息应显示:“抱歉,您尝试提取的金额超出了允许的透支限额”,而不是下面显示的内容。 Sample Output

【问题讨论】:

  • 我建议您退后一步,学习如何在运行时调试代码。以下是众多可用资源中的一些:Navigate through code with the Visual Studio debuggerLearn to debug C# code using Visual StudioDebugging C# Code in Visual Studio | Mosh
  • 您只想在这两行代码中捕获异常:double overdraftLimit = 1000; double dailyWithdrawlLimit = 1000; 将两个int值分配给doubles 不会引发异常。
  • 您需要重新评估您使用属性和字段的方式以及分配的内容。在您的属性设置器中,您实际上从未检查过value 或分配它。您的字段也是公开的。这个问题可能更适合Code Review
  • 我投票决定将此问题作为离题结束,因为它更适合Code Review
  • 请注意,即使您在 Main 方法中修复了变量/字段分配问题,您的异常仍然不会被抛出。异常抛出是属性设置器的一部分,但您的 Account 方法永远不会调用属性设置器(即,永远不会为相应的属性分配某些内容),因此不会抛出异常,因为属性设置器中的异常相关代码不执行。并不是说属性设置器中的逻辑一开始就很有意义,因为分配给属性的值只会被设置器忽略/丢弃......

标签: c# exception


【解决方案1】:

首先:您没有在方法withdraw()deposit() 中使用正确的名称。 它应该是Balance = Balance - amount 而不是balance = balance - amount。 第二:在Balance 的设置器中,您不使用设置器提供的新值。您可以通过关键字value 引用新值,也不要更新属性的支持字段。

【讨论】:

    猜你喜欢
    • 2021-08-24
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 2011-01-31
    • 2016-01-15
    • 2017-01-05
    • 2017-06-04
    相关资源
    最近更新 更多