• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's how your code should look:

 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic