从驱动程序类中捕获否定和字母输入异常

我正在尝试实现一个 try-catch 块来捕获否定和字符作为工资输入。

我已经在基类、驱动程序类、子类中尝试过 tc 块,但无法捕捉到这个错误的输入。

我试过使用双变量来确保“薪水/收入”输入实际上是错误的。

同样在打印 e1 实例时,第一个字符串值“firstName”总是打印 null。为什么会这样,用调试器检查它,它正确读取值但不打印它。

我会很感激你的帮助。

//FullTimeInstructor.java

package Employee;

public class FullTimeInstructor extends Employee {
private double weeklySalary;

public FullTimeInstructor(String firstName, String lastName, String taxFileNumber, double weeklySalary) {
    super(firstName, lastName, taxFileNumber);
    this.weeklySalary = weeklySalary;
            if (weeklySalary < 0) {
        throw new IllegalArgumentException("The salary cannot be a negative or a letter");
    } else if (weeklySalary != Double.NaN) 

/*tried this also... 
If (((Object)weeklySalary).getClass().getName()!="java.lang.Double")*/

{
        
throw new InputMismatchException("The salary cannot be a letter");

    }

}

public double getWeeklySalary() {
    return weeklySalary;
}

public void setWeeklySalary(double weeklySalary) {
    this.weeklySalary = weeklySalary;
}

@Override
public String toString() {
    return firstName + " " + lastName + " " + taxFileNumber + " " + weeklySalary + " " + 
Earnings();
}
public double Earnings(){
    return weeklySalary;
}
}

//employee.java

package Employee;

abstract class Employee {

public String firstName;
public String lastName;

public String taxFileNumber;

public Employee(String firstName, String lastName,String taxFileNumber) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.taxFileNumber = taxFileNumber;
  }

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getTaxFileNumber() {
    return taxFileNumber;
}

public void setTaxFileNumber(String taxFileNumber) {
    this.taxFileNumber = taxFileNumber;
}


public String toString() {
    return firstName + " " + lastName + " " + taxFileNumber;
}

public abstract double Earnings();
}

//EarningsTesting.java

package Employee;

import java.util.InputMismatchException;

public class EarningsTesting {


public static void main(String[] args) throws Exception { 
   
       
   try {

        FullTimeInstructor e1 = new FullTimeInstructor("Jane", "Smythe", "456DEF", 29000);
        System.out.println(e1);
        FullTimeInstructor e2 = new FullTimeInstructor("Janet", "Smith", "987TYR", 20000);
        System.out.println(e2);
        PartTimeInstructor e3 = new PartTimeInstructor("Alan", "Peters", "345AER", 25.0, 5.0, 20.0, 10.0);
        System.out.println(e3);
        PartTimeInstructor e4 = new PartTimeInstructor("Boris", "Johnson", "765PER", 25.0, 7.0, 20.0, 11.0);
        System.out.println(e4);
        SessionalInstructor e5 = new SessionalInstructor("Jeff", "Leyland", "555POI", 20.0, 5.0);
        System.out.println(e5);
        SessionalInstructor e6 = new SessionalInstructor("Jasmine", "Turner", "856YTE", 20.0, 6.0);
        System.out.println(e6);
    } catch (IllegalArgumentException e) {
        System.out.println(e.getMessage());
    } catch (InputMismatchException e) {
        System.out.println(e.getMessage());
    }
}
}

回答

Try-catch 结构用于捕获代码中已抛出的异常。如果要生成异常,请在构造函数中抛出异常:

if (weeklySalary < 0) {
    throw new IllegalArgumentException("The salary of "+ e1.getName() +" is negative");
}

然后,您可以使用 try - catch 块来捕获它以对其进行操作(例如):

try {
    FullTimeInstructor e1 = new FullTimeInstructor(someName, otherName, someId, negativeSalary);
} catch (IllegalArgumentException e) {
    // do something here with the invalid arguments
}


以上是从驱动程序类中捕获否定和字母输入异常的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>