Results 1 to 9 of 9

Thread: Exception error

  1. #1
    Rose88 is offline Member
    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0

    Lightbulb 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

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    can you post the errors as welll?
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote 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.

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    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.

  5. #5
    Join Date
    Jul 2009
    Posts
    4
    Rep Power
    0

    Default

    for ur doubts logon questionpapers.net

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote 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 . ;)

  7. #7
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    error should be catch block cant possibly catch a error, as no that kind of error throw in the try block

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Oh yes, you are correct. But even that code deals with the wrong exception, comes with an error, right?

  9. #9
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default 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
    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:
    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());
    	}
    }
    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. ;)
    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

  1. Exception error
    By jaiminparikh in forum New To Java
    Replies: 0
    Last Post: 03-20-2009, 09:06 PM
  2. Exception Error need help fixing
    By skinnybones in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 07:14 PM
  3. JSF error+exception
    By Peter in forum SWT / JFace
    Replies: 1
    Last Post: 07-04-2007, 06:29 AM
  4. Difference between error and exception
    By harinath chakrapani in forum New To Java
    Replies: 1
    Last Post: 06-19-2007, 08:49 AM
  5. tomcat exception-error
    By Nick15 in forum Eclipse
    Replies: 2
    Last Post: 05-11-2007, 01:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •