Results 1 to 9 of 9
Thread: Exception error
- 07-02-2009, 12:37 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
Exception error
Hey all,
I have an excption error!...it shows errors where is catch... I don't know why. please help me!
Java 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
- 07-02-2009, 12:58 AM #2
- 07-02-2009, 04:27 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
@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.
- 07-02-2009, 04:30 AM #4
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 13
no error throws, how can you catch it...
you should check the value of final balance before modify the variable balance...
like
Java 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 04:33 AM.
- 07-03-2009, 03:18 PM #5
Member
- Join Date
- Jul 2009
- Posts
- 4
- Rep Power
- 0
for ur doubts logon questionpapers.net
- 07-04-2009, 04:19 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
- 07-04-2009, 04:48 AM #7
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 13
error should be catch block cant possibly catch a error, as no that kind of error throw in the try block
- 07-04-2009, 04:52 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Oh yes, you are correct. But even that code deals with the wrong exception, comes with an error, right?
- 07-06-2009, 10:22 PM #9
Throwing exceptions
Hi Rose. :D
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:
/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
Java 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 [COLOR="RoyalBlue"]throw new overTheLimitException();[/COLOR] } } catch ([COLOR="RoyalBlue"]overTheLimitException[/COLOR] e) { System.err.println("Caught overTheLimitException: "+ e.toString()); } }
Good luck Rose. ;)Last edited by tim; 07-06-2009 at 10:24 PM.
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
Similar Threads
-
Exception error
By jaiminparikh in forum New To JavaReplies: 0Last Post: 03-20-2009, 09:06 PM -
Exception Error need help fixing
By skinnybones in forum New To JavaReplies: 2Last Post: 12-03-2007, 07:14 PM -
JSF error+exception
By Peter in forum SWT / JFaceReplies: 1Last Post: 07-04-2007, 06:29 AM -
Difference between error and exception
By harinath chakrapani in forum New To JavaReplies: 1Last Post: 06-19-2007, 08:49 AM -
tomcat exception-error
By Nick15 in forum EclipseReplies: 2Last Post: 05-11-2007, 01:32 AM
Bookmarks