Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
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?
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's how your code should look:

 
Knute Snortum
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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).
 
It was the best of times. It was the worst of times. It was a tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic