Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-02-2009, 01:37 AM
Member
 
Join Date: Apr 2009
Posts: 12
Rep Power: 0
Rose88 is on a distinguished road
Lightbulb Exception error
Hey all,

I have an excption error!...it shows errors where is catch... I don't know why. please help me!

Code:
public class overTheLimitException extends Exception {

	public overTheLimitException(){
			super ("Your Credit Card is over it's limit");
		}
}
public class NotEnoughMoneyException extends Exception {
	public  NotEnoughMoneyException(){
		super("The balance is under 0");
	}

}
public class CreditCard {
	/**
	 * String cardNumber
	 */
	protected String cardNumber;
	/**
	 * double balance
	 */
	protected double balance;
	/**
	 * int limit
	 */
	protected int limit;
	/**
	 * CreditCard constructor
	 * @param cardNumber
	 * @param balance
	 * @param limit
	 */
	public CreditCard(String cardNumber, double balance, int limit) {
		super();
		this.setCardNumber(cardNumber);
		this.setBalance(balance);
		this.setLimit(limit);
	}//end of constructor
	/**
	 * @return the cardNumber
	 */
	public String getCardNumber() {
		return cardNumber;
	}
	/**
	 * @param cardNumber the cardNumber to set
	 */
	public void setCardNumber(String cardNumber) {
		this.cardNumber = cardNumber;
	}
	/**
	 * @return the balance
	 */
	public double getBalance() {
		return balance;
	}
	/**
	 * the method updates CreditCard's Balance
	 * @param balance the balance to set
	 */
	public void setBalance(double bal) {
		this.balance = bal;
	}
	/**
	 * @return the limit
	 */
	public int getLimit() {
		return limit;
	}
	/**
	 * @param limit the limit to set
	 */
	public void setLimit(int limit) {
		this.limit = limit;
	}
	/**
	 * 
	 */
	public void chargeCard(double amount){
		try{
			if(limit<balance){
					if(amount<=balance){
						balance-=amount;
					}
			}
					}catch (NotEnoughMoneyException e){
						System.err.println("Caught NotEnoughMonetException: "+ e.toString());
					}catch(overTheLimitException e){
					System.err.println("Caught overTheLimitException: "+ e.toString());
				
				}
		}//ChargeCard
}//class
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 07-02-2009, 01:58 AM
angryboy's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Javaland
Posts: 742
Rep Power: 2
angryboy is on a distinguished road
Default
can you post the errors as welll?
__________________
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-02-2009, 05:27 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,499
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Originally Posted by angryboy View Post
can you post the errors as welll?

@Rose88, I don't think anyone wants to run your code and find the issues and fix it for you. So you have to give more details about your questions next time onwards, such as a code segment on your question and error messages if any. That's what angryboy wants to point here, I guess.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-02-2009, 05:30 AM
Senior Member
 
Join Date: Dec 2008
Location: Hong Kong
Posts: 473
Rep Power: 2
mtyoung is on a distinguished road
Default
no error throws, how can you catch it...

you should check the value of final balance before modify the variable balance...
like
Code:
if (balance - amount < 0)
   do something, may be throw error
else if ...
   do something, may be throw error
else
   balance = xxx

Last edited by mtyoung; 07-02-2009 at 05:33 AM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-03-2009, 04:18 PM
Member
 
Join Date: Jul 2009
Posts: 4
Rep Power: 0
rameshperumal1984 is on a distinguished road
Default
for ur doubts logon questionpapers.net
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-04-2009, 05:19 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,499
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Originally Posted by Rose88 View Post
I have an excption error!...it shows errors where is catch... I don't know why. please help me!
I think our thread starter comes with an error .
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-04-2009, 05:48 AM
Senior Member
 
Join Date: Dec 2008
Location: Hong Kong
Posts: 473
Rep Power: 2
mtyoung is on a distinguished road
Default
error should be catch block cant possibly catch a error, as no that kind of error throw in the try block
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-04-2009, 05:52 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,499
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Oh yes, you are correct. But even that code deals with the wrong exception, comes with an error, right?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-06-2009, 11:22 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 410
Rep Power: 3
tim is on a distinguished road
Default Throwing exceptions
Hi Rose.

An exception is not an error. The Exception and Error classes are subclasses of the Throwable class. You probably got the following problem when you tried to compile:
Quote:
/C:/Users/User/Desktop/CreditCard.java:73: exception NotEnoughMoneyException is never thrown in body of
corresponding try statement

}catch (NotEnoughMoneyException e){
^
/C:/Users/User/Desktop/CreditCard.java:75: exception overTheLimitException is never thrown in body of
corresponding try statement

}catch(overTheLimitException e){
^
2 errors
The compiler said that no exceptions are being thrown in your block of code. So you need to throw them according to your solution. So, let's say, the balance is negative when the person is in dept, and the limit is always positive. Then you could do this:
Code:
public void chargeCard(double amount){
	try {
		if(balance - amount >= 0 - limit){
			// it's okay to make more dept
			balance-=amount;
		} else {
			// the limit is reached
			throw new overTheLimitException();
		}
	} catch (overTheLimitException e) {
		System.err.println("Caught overTheLimitException: "+ e.toString());
	}
}
This example shows you how to throw an exception and how to recognize when no exceptions are being thrown, and you are trying to catch them.

Good luck Rose.
__________________
Eyes dwelling into the past are blind to what lies in the future.

Last edited by tim; 07-06-2009 at 11:24 PM.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception error jaiminparikh New To Java 0 03-20-2009 10:06 PM
Exception Error need help fixing skinnybones New To Java 2 12-03-2007 08:14 PM
JSF error+exception Peter SWT / JFace 1 07-04-2007 07:29 AM
Difference between error and exception harinath chakrapani New To Java 1 06-19-2007 09:49 AM
tomcat exception-error Nick15 Eclipse 2 05-11-2007 02:32 AM


All times are GMT +2. The time now is 11:52 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org