TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Maha
NA
0
325k
Creating own EXCEPTION CLASSES
Jan 30 2012 1:10 PM
This example is given to demonstrate creating your own EXCEPTION CLASSES. I wish to know importance of having static (highlighted in the example)
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);
}
}
}
//p303
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);
//OR throw(new NegativeBalanceException());
}
balance = bal;
}
}
public class NegativeBalanceException : ApplicationException
{
private
static
string msg = "Bank balance is negative.";
public NegativeBalanceException() : base(msg)
{
}
}
/*
Bank balance is negative.
at BankAccount.SetBalance(Double bal) in c:\documents and settings\winuser\my documents\my files\vif\visual c#\visual c# step by step\book chapters assignments\chapter8\dustbin 8\class1.cs:line 46
at TryBankAccount.Main() in c:\documents and settings\winuser\my documents\my files\vif\visual c#\visual c# step by step\book chapters assignments\chapter8\dustbin 8\class1.cs:line 13
Press any key to continue
*/
Reply
Answers (
30
)
GUI problem try #4
Classes and objects really hard