Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How To add my own Exceptions

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    26
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default How To add my own Exceptions

    hi i was wondering on how to add my own exception to the withdrawl method.

    import java.util.ArrayList;
    import java.util.List;
     
     
     
    public class BankManager implements _BankManager {
     
    	Storage store;
     
    	public BankManager() {
    		store = new ArrayListStorage();
     
    	}
     
    	public BankAccountOutputVO ViewAccountSummary(String accountNumber)
    			throws AccountDoesNotExist {
    		BankAccountOutputVO bao = null;
    		int accountNum = Integer.parseInt(accountNumber);
     
    		for (BankAccount acc : store.getList()) {
     
    			if (acc == null) {
    				//System.out.println("Account Does not exists");
    				throw new AccountDoesNotExist();
     
    			}
     
    			if (acc.getAccountNumber() == accountNum) {
    				store.getList();
    				bao = BankAccount.convertBAO(acc);
    			} else {
    				throw new AccountDoesNotExist();
    			}
    		}
    		return bao;
    	}
     
    	public BankAccountOutputVO closeAcount(String accountNumber) {
    		BankAccountOutputVO bao = null;
     
    		int accountNum = Integer.parseInt(accountNumber);
     
    		for (BankAccount acc : store.getList()) {
     
    			if (acc == null) {
    				// throw new AccountDoesNotExist();
    			}
     
    			if (acc.getAccountNumber() == accountNum) {
    				if (acc.balance == 0) {
    					store.getList().remove(acc);
    					bao = BankAccount.convertBAO(acc);
    				} else if (acc.getAccountNumber() == accountNum) {
    					bao = BankAccount.convertBAO(acc);
     
    					break;
    				}
    			}
    		}
    		return bao;
    	}
     
    	public BankAccountOutputVO createNewCurrentAccount(String name,
    			double amount, double overdraftLimit) {
    		BankAccountOutputVO bao = null;
    		BankAccount acc = new CurrentAccount(name, amount, overdraftLimit);
    		store.getList().add(acc);
    		bao = BankAccount.convertBAO(acc);
     
    		return bao;
     
    	}
     
    	public BankAccountOutputVO createNewSavingsAccount(String name,
    			double amount) {
     
    		BankAccountOutputVO bao = null;
    		BankAccount acc = new SavingAccount(name, amount, amount);
    		store.getList().add(acc);
    		bao = BankAccount.convertBAO(acc);
     
    		return bao;
    	}
     
    	public List<BankAccountOutputVO> listAllAccounts() {
     
    		ArrayList<BankAccountOutputVO> bao = new ArrayList<BankAccountOutputVO>();
     
    		for (BankAccount acc : store.getList()) {
    			bao.add(BankAccount.convertBAO(acc));
    		}
     
    		return bao;
    	}
     
    	public BankAccountOutputVO makeDeposit(String accountNumber, double amount) throws AccountDoesNotExist {
     
    		BankAccountOutputVO bao = null;
     
    		int accountNum = Integer.parseInt(accountNumber);
     
    		for (BankAccount acc : store.getList()) {
    			if (acc.getAccountNumber() == accountNum) {
    				acc.setdeposit(amount);
    				store.deposit(acc);
    				bao = BankAccount.convertBAO(acc);
    			}
    			else{
    				throw new AccountDoesNotExist();
    			}
     
     
    		}
    		return bao;
     
    	}
     
    	public BankAccountOutputVO makeWithdrawal(String accountNumber,
    			double amount) {
     
    		BankAccountOutputVO bao = null;
     
    		int accountNum = Integer.parseInt(accountNumber);
     
    		for (BankAccount acc : store.getList()) {
    			if (acc.getAccountNumber() == accountNum) {
    				if ((acc.getAccountType().equals("Saving Account"))
    						&& (acc.balance >= amount)) {
     
    					acc.setwithdrawl(amount);
    					store.withdraw(acc);
    					bao = BankAccount.convertBAO(acc);
    					break;
    				} else if ((acc.getAccountType().equals("Current Account"))
    						&& ((acc.balance + acc.overdraftLimit) >= amount)) {
    					acc.setwithdrawl(amount);
    					store.withdraw(acc);
    					bao = BankAccount.convertBAO(acc);
    					break;
    				} else {
    					bao = BankAccount.convertBAO(acc);
    					break;
    				}
     
    			}
     
    			if (acc == null) {
    				System.out.println("Account Does not avalible");
    				throw new NullPointerException();
     
    			}
    		}
     
    		return bao;
    	}
    }
     
    /highlight
     
    highlight=java
     
    class MakeWithdrawalAction implements _DisplayAction
    {
     
    	public void execute(UserInputVO input, _BankManager bank, _DisplayManager dm) 
    	{
     
    		BankAccountOutputVO output = null;
     
    		output = bank.makeWithdrawal(input
    				.getAccountNumber(), input.getAmount());
     
    		dm.AccountChange(output.getAccountNumber(), output.getName(), output.getBalance(),
    				output.getOverdraft());
    	}
    }
    Last edited by copeg; September 2nd, 2010 at 08:56 AM. Reason: Code tag formatting


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,123
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: How To add my own Exceptions

    First please use code tags around your code. See: Java Forums - BB Code List
    add my own exception to the withdrawl method.
    Where is the withdrawl method? There is one called: makeWithdrawal Is that it?

    You make your own exceptions by creating a class that extends the Exception class.
    Then you can create an instance of the class and throw that in the withdrawl method:
    throw new MyException("the message I want to pass");

Similar Threads

  1. Use of Exceptions and Methods of The String Class
    By cagataylina in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2011, 01:56 AM
  2. Java Exceptions
    By Vinceisg0d in forum Java Theory & Questions
    Replies: 2
    Last Post: March 12th, 2010, 11:25 PM
  3. Replies: 0
    Last Post: December 12th, 2009, 09:09 PM
  4. Replies: 2
    Last Post: November 19th, 2009, 10:55 PM