• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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: 7123
184
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: 7123
184
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: 7123
184
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).
 
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic