c#自定义例外
我无法让我的代码(下面)执行异常c#自定义例外,c#,exception,C#,Exception,我无法让我的代码(下面)执行异常 首先,银行账户是空的 预付1500英镑 尝试提取1050,但透支限额为1000 由于提取金额大于透支限额 OverdraftEx异常应显示我的消息(但不是) 然后尝试提取1000个,该操作应成功,并且 余额应该是500 现在,尝试提取50,但第二条异常消息DailyLimitEx应显示超出每日限额的信息,因为每日提取限额为1000,并且当天已提取,因此该异常再次未显示 然后,将显示一天的结束时间,该时间将每日限制重置为0 然后,因为这是新的一天,所以可以尝试撤回
样本输出: 注意:示例输出中的第一条错误消息应该显示:“对不起,您试图支取的金额超过了您允许的透支限额”,而不是下面显示的内容。
首先:您在方法中没有使用正确的名称
draw()
和deposit()
。
它应该是Balance=Balance-amount
,而不是Balance=Balance-amount
。
第二:在
Balance
的setter中,您不使用给定的新值。您可以通过关键字value
引用新值,也不更新属性的支持字段。我建议您后退一步,学习如何在运行时调试代码。以下是许多可用资源中的一些:,和。您只希望捕获这两行代码中的一个异常:double overdraftLimit=1000;每日双倍提款限额=1000代码>将两个int
值赋值给double
s不会引发异常。您需要重新评估如何使用属性和字段以及赋值是什么。在属性设置程序中,您从未实际检查或分配值。您的字段也是公共的。此问题可能更适合。我投票将此问题作为主题外的问题结束,因为它更适合。请注意,即使在Main方法中修复变量/字段分配问题,也不会抛出异常。异常抛出是属性设置器的一部分,但是您的Account方法从不调用属性设置器(即,从不将某些内容分配给相应的属性),因此不会抛出异常,因为属性设置器中与异常相关的代码没有执行。属性设置器中的逻辑一开始就没有多大意义,因为分配给属性的值将被设置器忽略/丢弃。。。
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;
}
}
}