Note that there are some explanatory texts on larger screens.

plurals
  1. PODIsplaying a message in an exception
    primarykey
    data
    text
    <p>I wrote some code testing out a BankAccount and I finished it (for the most part). The purpose was me attempting to learn how to throw and try/catch exceptions. For the most part I learned quite a bit. I realize this wasn't an "exceptional" case and if/else statements would have been more appropriate. I am just trying to learn exception handling. I am trying to write the exception classes to extend a class to include a new message detailing the error being sent when an exception is thrown. I read up on what a super class is, or base class in c#, and tried it. It compiles and looks right but I don't know what I am doing wrong. Can someone explain to me why my message in my throw new NegativeDepositException will not show.</p> <p>What I would like for it to do, is to throw a message instead of me putting a string in MessageBox.Show.</p> <pre><code>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 &lt; 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 &lt; 0) throw new NegativeWithdrawException(); OnWithdraw(this.amount, this.amount - a); this.amount -= a; if (this.amount &lt; 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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload