尝试使用窗体表单应用程序的catch语句

时间:2013-10-09 02:18:13

标签: c#

我的目标是在尝试将余额降至零以下时抛出NegativeBalanceException。就像这是一个真正的银行账户一样。我有if和else语句,但是把它们搞砸了,我试图学习尝试,捕获和抛出语句(最后也在阅读,但我不认为这适用于此)。无论如何,如果我只是点击存款按钮而没有输入任何内容,我设置了一个catch语句的位置。但是,我不明白它希望我将它实现在零以下的地方。这是我的存款方式吗?或者它是否在实际的btn_deposit中?另外,对if else语句使用try catch语句的目的是什么?我是编程新手,我只是想学习。

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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    public class BankAccount
    {
        decimal amount = 300.00m;
        // Declare Delegate Type Object
        public delegate void BankDelegate(decimal oldBalance, decimal newBalance);
        // Create Delegate Type Events 
        public event BankDelegate OnDeposit;
        public event BankDelegate OnWithdraw;

        public void Deposit(decimal a)
        {
            {
                if (a > 0)
                {
                OnDeposit(this.amount, this.amount + a);
                this.amount += a;
                }
                else
                {
                    MessageBox.Show("No!");
                }
            }
        }
        public void Withdraw(decimal a)
        {
            // Fire OnWithdraw Event and pass old and new balance amount
            OnWithdraw(this.amount, this.amount - a);
            this.amount -= a;
        }
    }
    // Declare BankAccount class variable 
    BankAccount account = null;
    private void Form1_Load(object sender, EventArgs e)
    {
        account = new BankAccount();
        // Attach Event Handlers with Events     
        account.OnDeposit += new BankAccount.BankDelegate(account_OnDeposit);
        account.OnWithdraw += new BankAccount.BankDelegate(account_OnWithdraw);
    }
    private void btnDeposit_Click(object sender, EventArgs e)
    {
        try
        {
                account.Deposit(Convert.ToDecimal(textBox1.Text));
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    private void btnWIthdraw_Click(object sender, EventArgs e)
    {
        account.Withdraw(Convert.ToDecimal(textBox1.Text));
    }
    void account_OnDeposit(decimal oldBalance, decimal newBalance)
    {
        label4.Text = oldBalance.ToString();
        label5.Text = newBalance.ToString();
    }
    void account_OnWithdraw(decimal oldBalance, decimal newBalance)
    {
        label4.Text = oldBalance.ToString();
        label5.Text = newBalance.ToString();
    }
}

}

1 个答案:

答案 0 :(得分:2)

您应该只针对特殊情况抛出异常。透支账户不是特殊情况..但存入负数是。

因此,我会为Deposit方法执行类似的操作:

public void Deposit(decimal a)
{
    if (a < 1)
        throw new NegativeDepositException("You cannot deposit this amount");

    OnDeposit(this.amount, this.amount + a);
    this.amount += a;
}

<强> BUT

您应该在输入方法之前验证这一点。这样,永远不应该调用异常 - 除非你在没有检查的情况下从另一个方法调用Deposit - 这将是例外。

private void btnDeposit_Click(object sender, EventArgs e)
{
    // try..catch removed. This will now crash if you forget to check the value.
    var amount = Convert.ToDecimal(textBox1.Text);
    if (amount < 1)
        MessageBox.Show("You cannot deposit this amount");
    else
        account.Deposit(amount);
}

另外,我会将Withdraw更改为bool,因为透支不是特殊情况:

public bool Withdraw(decimal a) 
{
    if (this.amount - a >= 0)
    {
        // Fire OnWithdraw Event and pass old and new balance amount
        OnWithdraw(this.amount, this.amount - a);
        this.amount -= a;
        return true; // successful
    }
    else
    {
        return false; // unsuccessful
    }
}

然后当你打电话时:

private void btnWIthdraw_Click(object sender, EventArgs e)
{
    if (!account.Withdraw(Convert.ToDecimal(textBox1.Text)))
    {
        MessageBox.Show("Insufficient funds");
    }
}

编辑:

回应你的评论。如果希望以特定方式命名(或者如果您希望它扩展普通Exception的功能),则必须创建自己的异常类。在我的示例中,您必须创建它:

public class NegativeDepositException : Exception {
}

多数民众赞成。它现在可以从Exception获得所需的一切。