class InvalidDepositException extends Exception{
public InvalidDepositException(String reason){
System.out.println(reason);
}
}
class AccountOverdrawnException extends Exception{
public AccountOverdrawnException(String reason){
System.out.println(reason);

}
}
public class Account{
private String saversName;
private String account;
private double currentBalance=0;

public void saveMoney(double savemoney){



if(savemoney<0){
throw new InvalidDepositException("存钱的金额不能小于0元");
      }
    else{
    this.currentBalance=savemoney;
System.out.println("你刚存了"+savemoney+"元钱");
}

}
public void pickMoney(double pickmoney){
if(pickmoney>currentBalance){
throw new AccountOverdrawnException("你要取的金额比帐户上的余额多");
}
else
System.out.println("你刚取了"+pickmoney+"元钱");

}
public void balanceInquiries(){
System.out.println("该用户当前余额为:  "+currentBalance);

}


public static void main(String args[]){
Account account1=new Account();

  
    account1.saveMoney(500);
account1.pickMoney(560);
account1.balanceInquiries();


}

}编译结果出错为:未报告的异常InvalidDepositException;必须对其扑获或声明以便抛出;
             未报告的异常AccountOverdrawnException;必须对其扑获或声明以便抛出;

解决方案 »

  1.   

    在方法体定义中,如果你要使用throw Exception这样的语法,那么你必须在方法定义时使用throws Exception语法,除非你要throw的Exception是RuntimeException类型.
    所以呢你需要更改你的方法声明 ...
      

  2.   

    非常感谢啊..~我还头疼了半天了..对了.RuntimeException类型可以的,,
    \可是我用在方法定义时使用throws Exception语法./还是不可以的 ./..\这是怎么回事呢././??
      

  3.   

    public static void main(String args[]){
    Account account1=new Account();  
        account1.saveMoney(500);
    account1.pickMoney(560);
    account1.balanceInquiries();
    } 加try catch或者public static void main(String args[]) throws Exception
      

  4.   

    throws Exception 是调用系统中的Exception...这里是自定义  的....>~>~>>~
      

  5.   

    class InvalidDepositException extends Exception {
    public InvalidDepositException(String reason) {
    System.out.println(reason);
    }
    }class AccountOverdrawnException extends Exception {
    public AccountOverdrawnException(String reason) {
    System.out.println(reason); }
    }public class Account {
    private String saversName; private String account; private double currentBalance = 0; public void saveMoney(double savemoney) throws InvalidDepositException { if (savemoney < 0) {
    throw new InvalidDepositException("存钱的金额不能小于0元");
    } else {
    this.currentBalance = savemoney;
    System.out.println("你刚存了" + savemoney + "元钱");
    } } public void pickMoney(double pickmoney) throws AccountOverdrawnException {
    if (pickmoney > currentBalance) {
    throw new AccountOverdrawnException("你要取的金额比帐户上的余额多");
    } else
    System.out.println("你刚取了" + pickmoney + "元钱"); } public void balanceInquiries() {
    System.out.println("该用户当前余额为:  " + currentBalance); } public static void main(String[] args) throws InvalidDepositException,
    AccountOverdrawnException {
    Account account1 = new Account(); account1.saveMoney(500);
    account1.pickMoney(560);
    account1.balanceInquiries(); }
    }
      

  6.   

    public static void main(String[] args)  {
    Account account1 = new Account(); try {
    account1.saveMoney(500);
    } catch (InvalidDepositException e) {

    e.printStackTrace();
    }
    try {
    account1.pickMoney(560);
    } catch (AccountOverdrawnException e) {

    e.printStackTrace();
    }
    account1.balanceInquiries(); }
      

  7.   


    class InvalidDepositException extends Exception {
    public InvalidDepositException(String reason) {
    System.out.println(reason);
    }
    } class AccountOverdrawnException extends Exception {
    public AccountOverdrawnException(String reason) {
    System.out.println(reason); }
    } public class Account {
    private String saversName; private String account; private double currentBalance = 0; public void saveMoney(double savemoney) throws InvalidDepositException { if (savemoney  < 0) {
    throw new InvalidDepositException("存钱的金额不能小于0元");
    } else {
    this.currentBalance = savemoney;
    System.out.println("你刚存了" + savemoney + "元钱");
    } } public void pickMoney(double pickmoney) throws AccountOverdrawnException {
    if (pickmoney  > currentBalance) {
    throw new AccountOverdrawnException("你要取的金额比帐户上的余额多");
    } else
    System.out.println("你刚取了" + pickmoney + "元钱"); } public void balanceInquiries() {
    System.out.println("该用户当前余额为:  " + currentBalance); } public static void main(String[] args) {
    Account account1 = new Account(); try {
    account1.saveMoney(500);
    } catch (InvalidDepositException e) {
    //e.printStackTrace();
    }
    try {
    account1.pickMoney(560);
    } catch (AccountOverdrawnException e) {
    //e.printStackTrace();
    }
    account1.balanceInquiries(); }
    }