0

I have a project in c# which is split into UI layer and Business layer. Basically I have a form where you can select an account and input a number for deposit. Once you click the OK button, your DepositTransaction.cs will handle the transaction.

Here is the sample code for DepositForm:

private void buttonOK_Click(object sender, EventArgs e) {
        try {
            bool inputTest;
            decimal amount;
            inputTest = decimal.TryParse(textBoxAmount.Text, out amount);
            if (inputTest == false) {
                throw new InvalidTransactionAmtException();
            } else {
                BankAccount account = comboBoxAccount.SelectedItem as BankAccount;
                deposit = new DepositTransaction(account, amount);
                this.DialogResult = DialogResult.OK;
            }
        } catch (InvalidTransactionAmtException ex) {
            errorProviderDeposit.SetError(textBoxAmount, ex.Message);
            textBoxAmount.Select();
            textBoxAmount.SelectAll();
        } catch (InvalidTransactionAmtNegativeException ex) {
            errorProviderDeposit.SetError(textBoxAmount, ex.Message);
            textBoxAmount.Select();
            textBoxAmount.SelectAll();
        } catch (AccountInactiveException ex) {
            errorProviderDeposit.SetError(textBoxAmount, ex.Message);
            textBoxAmount.Select();
            textBoxAmount.SelectAll();
        }
    }

And now the sample code for the DepositTransaction

public override void DoTransaction() {
        try {
            if (Amount <= 0) {   //Amount is the amount passed by the form
                throw new InvalidTransactionAmtNegativeException();
            }
            if (acc.Active == false) {    //acc is the account passed by the form
                throw new AccountInactiveException();
            }
            acc.Credit(Amount);
            Summary = string.Format("{0} {1}", Date.ToString("yyyy-MM-dd"), this.TransactionType);
            this.setStatus(TransactionStatus.Complete);
        } catch (InvalidTransactionAmtNegativeException ex) {
            throw;
        } catch (AccountInactiveException ex) {
            throw;
        }
    }

However, trying the above, does not pass the error to the Form. It just crashes the program saying that the exception was not handled.

I saw another question on stackoverflow that mentioned the way to pass the error is just to use throw: and that error will be passed to the class that called this class (in my case the form), and it will be handled in the form.

What am I doing wrong? Thank you

Iuli
  • 121
  • 1
  • 4
  • 12
  • 1
    Try 'throw new InvalidTransactionAmtNegativeException(message);' to set error message. – Prasanth Jun 22 '12 at 04:23
  • You should post the stack trace of your exception – tallseth Jun 22 '12 at 04:25
  • DoTransaction is likely throwing an exception that you are not catching in buttonOK_Click. You should add a catch(Exception ex) to catch all possible exceptions (unless you have a last chance handler elsewhere). By the way, the catch clauses at the end of DoTransaction are completely superfluous. – holmes Jun 22 '12 at 04:28
  • Thank you Prasanth, it worked. Actually just throw; also worked, is just the way I am handling the Deposit is different. Will update the question in a few minutes – Iuli Jun 22 '12 at 04:35
  • My DepositTransaction actually works a bit different as in it involves a timer. So I think the only way to check for lets say a negative amount would be to implement the checking into the actual form itself. – Iuli Jun 22 '12 at 04:43

3 Answers3

2

It just means that an exception that is neither of type InvalidTransactionAmtNegativeException nor AccountInactiveException is being thrown. Add new catch block

catch (Exception ex) {
    throw;
}

EDIT: You should have it come last. It will catch any other exceptions that might be thrown within your DoTransaction method

John Gathogo
  • 4,495
  • 3
  • 32
  • 48
0

You are repeating code in all your catch blocks in the UI, just use a generic catch block:

private void buttonOK_Click(object sender, EventArgs e) {
        try {
            bool inputTest;
            decimal amount;
            inputTest = decimal.TryParse(textBoxAmount.Text, out amount);
            if (inputTest == false) {
                throw new InvalidTransactionAmtException();
            } else {
                BankAccount account = comboBoxAccount.SelectedItem as BankAccount;
                deposit = new DepositTransaction(account, amount);
                deposit.DoTransaction();
                this.DialogResult = DialogResult.OK;
            }
        //catch any type of exception here
        } catch (Exception ex) {
            errorProviderDeposit.SetError(textBoxAmount, ex.Message);
            textBoxAmount.Select();
            textBoxAmount.SelectAll();
        }
    }
tallseth
  • 3,635
  • 1
  • 23
  • 24
0

It Seems that your exception doesn't comes under the specific exception you have given in catch block. So catch generic exception at the end. It is a good practice.

Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42