• Post Reply Bookmark Topic Watch Topic
  • New Topic

unreported exception InvalidWithdrawalException; must be caught or declared to be thrown w.withDraw  RSS feed

 
moaid najem
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have done the code but the error unreported exception InvalidWithdrawalException; must be caught or declared to be thrown
w.withDrawal(200.00, 100.00); shown

my code is

import java.util.*;



class InvalidWithdrawalException extends Exception


{


public InvalidWithdrawalException()

{


}

public InvalidWithdrawalException(String message)

{

super(message);

}

public InvalidWithdrawalException(Throwable cause)

{

super(cause);

}

}


class AccountServer

{

double balance, withdrawal, deposit;


public static void withDrawal(double withdrawal, double balance) throws InvalidWithdrawalException

{

if(withdrawal > balance){


throw new InvalidWithdrawalException();}

}





public static void transaction(double deposit, double withdrawal, double balance) throws IllegalArgumentException


{

if (deposit <0 || withdrawal < 0)

throw new IllegalArgumentException ();

}
}

class CustomerClient

{

String customerName;

long accountNo;
double newb, balance, withdrawal, deposit;


public void customerWithdrawal()

{



try
{
AccountServer n = new AccountServer();
n.withDrawal(withdrawal, balance);

}

catch (InvalidWithdrawalException e)

{

System.out.println("your balance is low");

}

}


public void customerDeposit()

{


try

{

newb = deposit + balance;
System.out.println("new balance: " + newb);

}

catch (IllegalArgumentException e)

{

e.printStackTrace();

}

}

}

public class AccountManager
{

public static void main(String[] args)
{
//Scanner scan = new Scanner( System.in );
AccountServer w = new AccountServer();
AccountServer d = new AccountServer();

w.withDrawal(200.00, 100.00);
d.transaction(-200.00, -100.00, 300.00);
//transaction(deposit);
}

}

any help?
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

That error means that the exception that withDrawal() throws must either be caught with a try/catch block or declared to be thrown by main().

Now a few words about displaying code. Always UseCodeTags (that's a link). It makes the code more readable. You should also get rid of most of the blank lines. Leave only a blank line that helps group together important lines, like between methods. And last but not least, make sure your indentation is correct. This isn't just because it looks nice; it shows correct structure.
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's how your code should look:

 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some other problems with your code. The methods withDrawal() and transaction() shouldn't be static, since you're accessing them with objects. Making a method static means it belongs to the class, not an instance of the class (object).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!