I need help with error handling

No description
10 Replies
AppleCyder (NovaSprite)
This is what I have... Exception:
namespace Exception
{
public class NegativeNumberException(string error) : base(error)
{
}
}

public class Exception
{
// variables
private decimal Balance;
private String AccountName;
private int AccountNumber;

// getters/setters
public decimal getBalance() { return Balance; }
public void setBalance(decimal blc)
{
if (blc < 0) Balance = 0;
else Balance = blc;
}
public String getAccountName() { return AccountName; }
public void setAccountName(String an) { AccountName = an; }
public int getAccountNumber() { return AccountNumber; }
public void setAccountNumber(int an) { AccountNumber = an; }

// constructors
public Exception(String aname, int anumber, decimal abalance)
{
setAccountName(aname);
setAccountNumber(anumber);
setBalance(abalance);
}

// methods
public virtual bool Credit(decimal abalance) { setBalance(getBalance() + abalance); return true; }
public virtual bool Debit(decimal abalance)
{
if (abalance > getBalance()) { Console.Write("\nInsufficient Funds"); return false; }
else setBalance(getBalance() - abalance);
return true;
}
public virtual void PrintAccount()
{
Console.WriteLine("\nAccount Name : {0}", getAccountName());
Console.WriteLine("Account Number : {0}", getAccountNumber());
Console.WriteLine("Balance : ${0}", getBalance());
}
}
namespace Exception
{
public class NegativeNumberException(string error) : base(error)
{
}
}

public class Exception
{
// variables
private decimal Balance;
private String AccountName;
private int AccountNumber;

// getters/setters
public decimal getBalance() { return Balance; }
public void setBalance(decimal blc)
{
if (blc < 0) Balance = 0;
else Balance = blc;
}
public String getAccountName() { return AccountName; }
public void setAccountName(String an) { AccountName = an; }
public int getAccountNumber() { return AccountNumber; }
public void setAccountNumber(int an) { AccountNumber = an; }

// constructors
public Exception(String aname, int anumber, decimal abalance)
{
setAccountName(aname);
setAccountNumber(anumber);
setBalance(abalance);
}

// methods
public virtual bool Credit(decimal abalance) { setBalance(getBalance() + abalance); return true; }
public virtual bool Debit(decimal abalance)
{
if (abalance > getBalance()) { Console.Write("\nInsufficient Funds"); return false; }
else setBalance(getBalance() - abalance);
return true;
}
public virtual void PrintAccount()
{
Console.WriteLine("\nAccount Name : {0}", getAccountName());
Console.WriteLine("Account Number : {0}", getAccountNumber());
Console.WriteLine("Balance : ${0}", getBalance());
}
}
SavingsAccount:
public class SavingsAccount : Exception
{
// variables
private double InterestRate;

// getters/setters
public void setInterestRate(double interestRate)
{
if (interestRate < 0) InterestRate = 0;
{
throw new NegativeNumberException("Invalid Entry – Negative numbers are not permitted. Please enter a non-negative value.");
}
else
{
InterestRate = interestRate;
}
}
public double getInterestRate() { return InterestRate; }

// constructors
public SavingsAccount(String aname, int anumber, decimal abalance, double asinterestrate) : base(aname, anumber, abalance)
{
setInterestRate(asinterestrate);
}

// methods
public decimal CalculateInterest()
{
return (decimal)getInterestRate() * getBalance();
}

// Override methods
public override void PrintAccount()
{
base.PrintAccount();
Console.Write("Interest rate : {0}%", getInterestRate() * 100);
}
}
public class SavingsAccount : Exception
{
// variables
private double InterestRate;

// getters/setters
public void setInterestRate(double interestRate)
{
if (interestRate < 0) InterestRate = 0;
{
throw new NegativeNumberException("Invalid Entry – Negative numbers are not permitted. Please enter a non-negative value.");
}
else
{
InterestRate = interestRate;
}
}
public double getInterestRate() { return InterestRate; }

// constructors
public SavingsAccount(String aname, int anumber, decimal abalance, double asinterestrate) : base(aname, anumber, abalance)
{
setInterestRate(asinterestrate);
}

// methods
public decimal CalculateInterest()
{
return (decimal)getInterestRate() * getBalance();
}

// Override methods
public override void PrintAccount()
{
base.PrintAccount();
Console.Write("Interest rate : {0}%", getInterestRate() * 100);
}
}
CheckingAccount:
public class CheckingAccount : Exception
{
// variables
private decimal FeeCharged;

// getters/setters
public void setFeeAmount(decimal feeamount)
{
if (feeamount < 0) FeeCharged = 0;
{
throw new NegativeNumberException("Invalid Entry – Negative numbers are not permitted. Please enter a non-negative value");
}
else FeeCharged = feeamount;

}
public decimal getFeeAmount() { return FeeCharged; }

// constructors
public CheckingAccount(String aname, int anumber, decimal abalance, decimal feeamount) : base(aname, anumber, abalance)
{
setFeeAmount(feeamount);
}

// methods
public override bool Credit(decimal abalance)
{
if (abalance <= base.getBalance())
{
base.Credit(abalance);
base.setBalance(base.getBalance() - getFeeAmount());
return true;
}
else return false;
}

public override bool Debit(decimal abalance)
{
bool test = base.Debit(abalance);
if (test) { base.setBalance(base.getBalance() - getFeeAmount()); return true; }
else return false;
}
public override void PrintAccount()
{
base.PrintAccount();
Console.Write("Fee charged : ${0}", getFeeAmount());
}
}
public class CheckingAccount : Exception
{
// variables
private decimal FeeCharged;

// getters/setters
public void setFeeAmount(decimal feeamount)
{
if (feeamount < 0) FeeCharged = 0;
{
throw new NegativeNumberException("Invalid Entry – Negative numbers are not permitted. Please enter a non-negative value");
}
else FeeCharged = feeamount;

}
public decimal getFeeAmount() { return FeeCharged; }

// constructors
public CheckingAccount(String aname, int anumber, decimal abalance, decimal feeamount) : base(aname, anumber, abalance)
{
setFeeAmount(feeamount);
}

// methods
public override bool Credit(decimal abalance)
{
if (abalance <= base.getBalance())
{
base.Credit(abalance);
base.setBalance(base.getBalance() - getFeeAmount());
return true;
}
else return false;
}

public override bool Debit(decimal abalance)
{
bool test = base.Debit(abalance);
if (test) { base.setBalance(base.getBalance() - getFeeAmount()); return true; }
else return false;
}
public override void PrintAccount()
{
base.PrintAccount();
Console.Write("Fee charged : ${0}", getFeeAmount());
}
}
internal class program (I don't even know if this would be necessary for this code):
internal class Program
{
static void Main(string[] args)
{
Console.Write("Created account with $1,000 balance.\n"); // 1
CheckingAccount cAccount = new CheckingAccount("Abel" + "-Checking", 1, 1000, (decimal)3.00); // 1

Console.Write("\nCreated account with $2,000 balance.\n"); // 2
SavingsAccount sAccount = new SavingsAccount("Abel" + "-Savings", 2, 2000, 0.05); // 2


cAccount.PrintAccount(); // 3
Console.Write("\n");
sAccount.PrintAccount(); // 4

Console.Write("\n\nDeposit $100 into checking."); // 5
cAccount.Credit(100); // 5

cAccount.PrintAccount(); // 6

Console.Write("\n\nWithdraw $50 from checking."); // 7
cAccount.Debit(50); // 7

cAccount.PrintAccount(); // 8

Console.Write("\n\nWithdraw $6,000 from checking."); // 9
cAccount.Debit(6000); // 9

cAccount.PrintAccount(); // 10

Console.Write("\n\nDeposit $3,000 into savings."); // 11
sAccount.Credit(3000); // 11

sAccount.PrintAccount(); // 12

Console.Write("\n\nWithdraw $200 from savings."); // 13
sAccount.Debit(200); // 13

sAccount.PrintAccount(); // 14

Console.Write("\n\nCalculate interests on savings."); // 15
sAccount.setBalance(sAccount.getBalance() + sAccount.CalculateInterest()); // 15

sAccount.PrintAccount(); // 16

Console.Write("\n\nWithdraw $10,000 from savings."); // 17
sAccount.Debit(10000); // 17

sAccount.PrintAccount(); // 18


}

}
internal class Program
{
static void Main(string[] args)
{
Console.Write("Created account with $1,000 balance.\n"); // 1
CheckingAccount cAccount = new CheckingAccount("Abel" + "-Checking", 1, 1000, (decimal)3.00); // 1

Console.Write("\nCreated account with $2,000 balance.\n"); // 2
SavingsAccount sAccount = new SavingsAccount("Abel" + "-Savings", 2, 2000, 0.05); // 2


cAccount.PrintAccount(); // 3
Console.Write("\n");
sAccount.PrintAccount(); // 4

Console.Write("\n\nDeposit $100 into checking."); // 5
cAccount.Credit(100); // 5

cAccount.PrintAccount(); // 6

Console.Write("\n\nWithdraw $50 from checking."); // 7
cAccount.Debit(50); // 7

cAccount.PrintAccount(); // 8

Console.Write("\n\nWithdraw $6,000 from checking."); // 9
cAccount.Debit(6000); // 9

cAccount.PrintAccount(); // 10

Console.Write("\n\nDeposit $3,000 into savings."); // 11
sAccount.Credit(3000); // 11

sAccount.PrintAccount(); // 12

Console.Write("\n\nWithdraw $200 from savings."); // 13
sAccount.Debit(200); // 13

sAccount.PrintAccount(); // 14

Console.Write("\n\nCalculate interests on savings."); // 15
sAccount.setBalance(sAccount.getBalance() + sAccount.CalculateInterest()); // 15

sAccount.PrintAccount(); // 16

Console.Write("\n\nWithdraw $10,000 from savings."); // 17
sAccount.Debit(10000); // 17

sAccount.PrintAccount(); // 18


}

}
Menu:
public class Menu
{
static void Main(string[] args)
{
String choice;
{
Console.WriteLine("------------------------------");
Console.WritLine("Create Checking Account C");
Console.WritLine("Create Savings Account S");
Console.WritLine("Quit Application Q");
Console.WritLine("------------------------------");

Console.WritLine("Enter Choice");
choice = Console.ReadLine();

switch
{
case "C".
MenuCheckingAccount();
break;

case "S".
MenuSavingsAccount();
break

case "Q".
Console.WriteLine("Quitting menu...");
}

Console.WriteLine();
while (choice != Q) ;
}
public class Menu
{
static void Main(string[] args)
{
String choice;
{
Console.WriteLine("------------------------------");
Console.WritLine("Create Checking Account C");
Console.WritLine("Create Savings Account S");
Console.WritLine("Quit Application Q");
Console.WritLine("------------------------------");

Console.WritLine("Enter Choice");
choice = Console.ReadLine();

switch
{
case "C".
MenuCheckingAccount();
break;

case "S".
MenuSavingsAccount();
break

case "Q".
Console.WriteLine("Quitting menu...");
}

Console.WriteLine();
while (choice != Q) ;
}
MenuCheckingAccount:
public class MenuCheckingAccount
{
Console.Writeline("Create Checking Account");
Console.Writeline("-------------------");

Console.WriteLine("Enter a name for the account: ");
string aname = Console.ReadLine();

Console.WriteLine("Enter an account number: ");
int anumber;
if (!)//letters are entered instead of numbers...
{
Console.WriteLine("Invalid account number.");
return;
}

Console.WriteLine("Enter an initial balance for the account: ");
decimal abalance;
if
{
Console.WriteLine("Invaid initial balance.");
return;
}

{
CheckingAccount checkingAccount = new CheckingAccount(aname, anumber, abalance);
Console.WriteLine();
checkingAccount.PrintAccount();
}

Catch (NegativeNumberException)
{
Console.WriteLine();//dispays the error message
}
public class MenuCheckingAccount
{
Console.Writeline("Create Checking Account");
Console.Writeline("-------------------");

Console.WriteLine("Enter a name for the account: ");
string aname = Console.ReadLine();

Console.WriteLine("Enter an account number: ");
int anumber;
if (!)//letters are entered instead of numbers...
{
Console.WriteLine("Invalid account number.");
return;
}

Console.WriteLine("Enter an initial balance for the account: ");
decimal abalance;
if
{
Console.WriteLine("Invaid initial balance.");
return;
}

{
CheckingAccount checkingAccount = new CheckingAccount(aname, anumber, abalance);
Console.WriteLine();
checkingAccount.PrintAccount();
}

Catch (NegativeNumberException)
{
Console.WriteLine();//dispays the error message
}
MenuSavingsAccount:
public class MenuSavingsAccount
{
Console.Writeline("Create Saving Account");
Console.Writeline("-------------------");


Console.WriteLine("Enter a name for the account: ");
string aname = Console.ReadLine();

Console.WriteLine("Enter an account number: ");
int anumber;
if(!)//letters are entered instead of numbers...
{
Console.WriteLine("Invalid account number.");
return;
}

Console.WriteLine("Enter an initial balance for the account: ");
decimal interestRate;
if(!)
{
Console.WriteLine("Invaid initial balance.");
return;
}
{
SavingsAccount savingsAccount = new SavingsAccount(aname, anumber, abalance);
Console.WriteLine;
savingsAccount.PrintAccount();
}

CatchBlock (NegativeNumberException)
Console.WriteLine();

Console.ReadKey();
}
}
public class MenuSavingsAccount
{
Console.Writeline("Create Saving Account");
Console.Writeline("-------------------");


Console.WriteLine("Enter a name for the account: ");
string aname = Console.ReadLine();

Console.WriteLine("Enter an account number: ");
int anumber;
if(!)//letters are entered instead of numbers...
{
Console.WriteLine("Invalid account number.");
return;
}

Console.WriteLine("Enter an initial balance for the account: ");
decimal interestRate;
if(!)
{
Console.WriteLine("Invaid initial balance.");
return;
}
{
SavingsAccount savingsAccount = new SavingsAccount(aname, anumber, abalance);
Console.WriteLine;
savingsAccount.PrintAccount();
}

CatchBlock (NegativeNumberException)
Console.WriteLine();

Console.ReadKey();
}
}
SinFluxx
SinFluxx2y ago
If it won't run I assume your IDE is showing some errors?
AppleCyder (NovaSprite)
Yes, I’ll share them in a bit, do you understand what I’m trying to accomplish tho?
becquerel
becquerel2y ago
i will mention just from a glance at your code that the way you're defining your custom exception seems suspect rather than defining your own class called Exception, your NegativeNumberException should inherit from the pre-existing System.Exception class that will allow you to 'throw' instances of NegativeNumberException
CatchBlock (NegativeNumberException)
this is also incorrect syntax
Catch (NegativeNumberException) { Console.WriteLine();//dispays the error message }
this will also not work; this is the right syntax:
catch (NegativeNumberException ex) { Console.WriteLine(ex); }
AppleCyder (NovaSprite)
There are wayyy more errors than shown here, do I show ALL of it?
No description
AppleCyder (NovaSprite)
Are you saying that I should put my custom exception under my first public class?
becquerel
becquerel2y ago
No. The way you define a custom exception in C# is by inheriting from the predefined System.Exception class, which already exists by default.
using System; public class MyCoolException : System.Exception { } public void SomeMethod() { // Because I inherited from System.Exception, I can 'throw' my custom exception. throw new MyCoolException(); }
I don't know why you made your own class called Exception. When you get errors like these it's because you made a syntax mistake and the compiler can't figure out anything about your program, so there's not much point caring about all the errors it spits out. Check the curly braces in your Program.cs all match up with each other and you haven't forgotten a semicolon anywhere.
AppleCyder (NovaSprite)
Ok 👍 I want to focus on the switch statements, is there something wrong with the syntax or something?
Pobiega
Pobiega2y ago
yes.
case "Q".
Console.WriteLine("Quitting menu...");
}

Console.WriteLine();
while (choice != Q) ;
}
case "Q".
Console.WriteLine("Quitting menu...");
}

Console.WriteLine();
while (choice != Q) ;
}
that closing bracket on the third line is.. uh... probably not supposed to be there? and . is invalid all cases should end with either break or return
AppleCyder (NovaSprite)
switch (choice)
{
case "C":
MenuCheckingAccount();
break;

case "S":
MenuSavingsAccount();
break

case "Q":
Console.WriteLine("Quitting menu...");
break;
}

Console.WriteLine();
while (choice != "Q") ;
Console.WriteLine("Press any key to exit menu...");

}
switch (choice)
{
case "C":
MenuCheckingAccount();
break;

case "S":
MenuSavingsAccount();
break

case "Q":
Console.WriteLine("Quitting menu...");
break;
}

Console.WriteLine();
while (choice != "Q") ;
Console.WriteLine("Press any key to exit menu...");

}
so like this? Also, in MenuCheckingAccount, next to the "if" statement, what would this do...
(!int.TryParse(Console.ReadLine(), out anumber))
(!int.TryParse(Console.ReadLine(), out anumber))
if I were to put it here where (!) is
Console.WriteLine("Enter an account number: ");
int anumber;
if (!)//letters are entered instead of numbers...
{
Console.WriteLine("Invalid account number.");
return;
}
Console.WriteLine("Enter an account number: ");
int anumber;
if (!)//letters are entered instead of numbers...
{
Console.WriteLine("Invalid account number.");
return;
}
it would look like this
Console.WriteLine("Enter an account number: ");
int anumber;
if (!int.TryParse(Console.ReadLine(), out anumber)//letters are entered instead of numbers...
{
Console.WriteLine("Invalid account number.");
return;
}
Console.WriteLine("Enter an account number: ");
int anumber;
if (!int.TryParse(Console.ReadLine(), out anumber)//letters are entered instead of numbers...
{
Console.WriteLine("Invalid account number.");
return;
}

Did you find this page helpful?