3
Answers

Exception

Maha

Maha

11y
1.1k
1
There is a statement in a book saying that "Every Exception object contains a ToString() method and a Message field". Message field is obvious this is highlighted in the following program. Please explain what is meant by ToString() method.

using System;
public class TryBankAccount
{
public static void Main()
{
BankAccount acct = new BankAccount();

try
{
acct.SetAccountNum(1234);
acct.SetBalance(-1000);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}

Console.ReadKey();
}
}
public class BankAccount
{
private int accountNum;
private double balance;

public int GetAccountNum()
{
return accountNum;
}
public void SetAccountNum(int acctNumber)
{
accountNum = acctNumber;
}

public double GetBalance()
{
return balance;
}
public void SetBalance(double bal)
{
if (bal < 0)
{
NegativeBalanceException nbe = new NegativeBalanceException();
throw (nbe);
}
balance = bal;
}
}
public class NegativeBalanceException : ApplicationException
{
private static string msg = "Bank balance is negative.";

public NegativeBalanceException() : base(msg)
{
}
}

Answers (3)