Search code examples
c#error-handlingtry-catchrethrow

How to pass a thrown exception from a C# class to a C# Form


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


Solution

  • 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