【问题标题】:No exception caught?没有发现异常?
【发布时间】:2011-11-26 09:10:59
【问题描述】:

我对 java 很陌生,在让我的代码捕获和显示异常时遇到了一些问题。代码的想法是,如果 Loan 的任何值为零或更小,则抛出异常。下面是我的代码,尽管loan1 和loan3 只有零,但它没有任何异常输出。六周的新手将不胜感激任何帮助。谢谢!

package loan;

public class Loan {

    public static void main(String[] args) {
        try {
        Loan loan1 = new Loan(0, 0, 00.00);
        Loan loan2 = new Loan(5, 10, 500.00);
        Loan loan3 = new Loan(0, 0, 0.00);

        }
        catch (IllegalArgumentException ex) {
                System.out.println(ex);
        }
    }


    // declare variables for Loan class
    double annualInterestRate;
    int numberOfYears;
    double loanAmount;
    private java.util.Date loanDate;

    // Loan class
    public Loan() {
    }
    public Loan(double annualInterestRate, int numberOfYears, double loanAmount) {
        this.annualInterestRate = annualInterestRate;
        this.numberOfYears = numberOfYears;
        this.loanAmount = loanAmount;
        loanDate = new java.util.Date();
    }


    public double getAnnualInterestRate() throws IllegalArgumentException {              //exception added if value is zero or less, added on each variable
        if (annualInterestRate >= 0)
        throw new IllegalArgumentException("Interest rate cannot be zero or          negative.");
        else
            return annualInterestRate;

    }

    public void setAnnualInterestRate(double annualInterestRate) {
        this.annualInterestRate = annualInterestRate;
    }

    public int getNumberOfYears() throws IllegalArgumentException {
        if (numberOfYears >= 0)
            return numberOfYears;
        else
            throw new IllegalArgumentException("Number of years cannot be zero");
        }

    public void setNumberOfYears(int numberOfYears) {
        this.numberOfYears = numberOfYears;
    }

    public double getLoanAmount() throws IllegalArgumentException {
        if (loanAmount >= 0)
            return loanAmount;
        else
            throw new IllegalArgumentException("Loan amount cannot be zero");
    }

    public void setLoanAmount(double loanAmount) {
        this.loanAmount = loanAmount;
    }

    public double getMonthlyPayment() {
        double monthlyInterestRate = annualInterestRate/1200;
        double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (Math.pow(1/(1 + monthlyInterestRate), numberOfYears *12)));
        return monthlyPayment;
        }
    public double getTotalPayment() {
        double totalPayment = getMonthlyPayment() * numberOfYears * 12;
        return totalPayment;
    }

    public java.util.Date getLoanDate() {
        return loanDate;
    }
}

【问题讨论】:

    标签: java exception exception-handling


    【解决方案1】:

    如果您确实希望构造函数在某些情况下抛出异常,您应该如下所示:

    public Loan(double annualInterestRate, int numberOfYears, double loanAmount) {
        if ( annualInterestRate<0 ) 
            throw new IllegalArgumentException( "value for anualInterestRate is too small" );
        if ( numberOfYears<0 ) 
            throw new IllegalArgumentException( "value for numberOfYears is too small" );
        this.annualInterestRate = annualInterestRate;
        this.numberOfYears = numberOfYears;
        this.loanAmount = loanAmount;
        loanDate = new java.util.Date();
    }
    

    【讨论】:

      【解决方案2】:

      您只在“getter”方法中抛出异常,例如getAnnualInterestRate 等。但您从不调用它们。要么调用你的一些 getter,要么(更好)从构造函数和 setter 方法中抛出异常(因为这是你设置值的地方)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-11
        • 2019-01-25
        • 1970-01-01
        • 1970-01-01
        • 2014-12-02
        • 1970-01-01
        • 2011-10-13
        • 1970-01-01
        相关资源
        最近更新 更多