简体   繁体   English

没有发现异常吗?

[英]No exception caught?

I am very new to java and I am having some issues with getting my code to catch and display an exception has been caught. 我对Java非常陌生,但在捕获代码和显示异常方面遇到了一些问题。 The idea of the code is to throw an exception if any of the values of Loan are zero or less. 代码的想法是,如果Loan的任何值等于或小于零,则引发异常。 Below is my code, which does not have any output for an exception, despite the fact that loan1 and loan3 have nothing but zeros. 以下是我的代码,尽管loan1和loan3除了零外什么都没有,但没有任何异常输出。 Any help would be appreciated by a six week newbie. 六个星期的新手将不胜感激。 Thanks! 谢谢!

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;
    }
}

if you want indeed want your constructor to throw an exception in some condition, you should look like this: 如果确实希望构造函数在某种情况下引发异常,则应如下所示:

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();
}

You only throw an exception in the "getter" methods, like getAnnualInterestRate , etc. But you never call them. 您只能在“ getter”方法中引发异常,例如getAnnualInterestRate等。但是您永远不会调用它们。 Either call some of your getters, or (better) throw an exception from the constructor and the setter methods (because this is where you're setting the values). 要么调用您的一些getter,要么(更好)从构造函数和setter方法中引发异常(因为这是您在其中设置值的地方)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM