2013-10-13 153 views
1

我編寫了一些測試BankAccount的代碼,並完成了它(大部分)。 我的目的是試圖學習如何拋出並嘗試/捕獲異常。大部分我學到了很多。我意識到這不是一個「例外」的情況,如果/ else的話會更合適。我只是想學習異常處理。 我正在嘗試編寫異常類來擴展一個類,以包含一條新消息,詳細描述引發異常時發送的錯誤。 我讀了一個超類是什麼,或在C#中的基類,並嘗試過它。它編譯和看起來很正確,但我不知道我做錯了什麼。有人可以向我解釋爲什麼我的消息在我的拋出新NegativeDepositException不會顯示。在異常中顯示消息

我想要它做的是拋出一條消息,而不是我在MessageBox.Show中放置一個字符串。

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 class NegativeBalanceException : Exception { } 
     public class NegativeWithdrawException : Exception { } 
     public class NegativeDepositException : Exception 
     { 
      public NegativeDepositException() { } // this doesn't do what I would like for it to do. 
      public NegativeDepositException(string message) 
       : base(message) { } 
     } 
    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) 
       throw new NegativeDepositException(); //"type some message here instead of messagebox below"); 
      OnDeposit(this.amount, this.amount + a); 
      this.amount += a; 
     } 
     public void Withdraw(decimal a) 
     { 
      if (a < 0) 
       throw new NegativeWithdrawException(); 
      OnWithdraw(this.amount, this.amount - a); 
      this.amount -= a; 
      if (this.amount < 0) 
       throw new NegativeBalanceException(); 
     } 
    } 
    // this is my bank class variable... 
    BankAccount account = null; 
    private void Form1_Load(object sender, EventArgs e) 
    { 
     account = new BankAccount(); 
     account.OnDeposit += new BankAccount.BankDelegate(account_OnDeposit); 
     account.OnWithdraw += new BankAccount.BankDelegate(account_OnWithdraw); 
    } 
    private void btnDeposit_Click(object sender, EventArgs e) 
    { 
     { 
      var amount = Convert.ToDecimal(textBox1.Text); 
      try { account.Deposit(amount); } 
      catch (NegativeDepositException) 
      { 
       MessageBox.Show("Cannot deposit negative amounts"); 
      } 
     } 
    } 
    private void btnWIthdraw_Click(object sender, EventArgs e) 
    { 
     var amount = Convert.ToDecimal(textBox1.Text); 
     try { account.Withdraw(amount); } 
     catch (NegativeBalanceException) 
     { 
      MessageBox.Show("Cannot withdraw money that isn't there"); 
     } 
     catch (NegativeWithdrawException) 
     { 
      MessageBox.Show("Cannot withdraw negative money"); 
     } 
    } 
    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(); 
    } 
} 

回答

0

也許我誤解了這個問題,但是在拋出異常時沒有傳遞消息;

它應該是:

if (a < 0) 
    throw new NegativeDepositException("Cannot deposit negative amounts"); 

和追趕時,它需要調用異常的Message屬性(這是在異常基類)

catch (NegativeWithdrawException exception) 
    { 
     MessageBox.Show(exception.Message); 
    } 
+0

從本質上講是的,它是不通過。那個(exception.Message)有錯誤;主要是引用了我需要的引用,當我添加它們時,它會繼續出現更多的錯誤。如果可能, – Zoro

+0

發佈編譯錯誤。 – Nefarion