需要帮助捕获 For Each 循环上的 ArrayIndexOutOfBoundsException (Java)

问题描述 投票:0回答:1

HW3.java

导入java.util.Scanner;

公开课 HW3 {

//Method Establishes Scanner For Inputing Hour
public static int Scanner() {
    Scanner x = new Scanner(System.in);
    int pay = x.nextInt();
    return pay;
}
        
public static void main(String args[]) {
    //Asks For Hour Input, Input Hours Into Variable
    System.out.println("Input Hours");
    int hour = Scanner();

    //Establish Array Of Employees With Name And Wage Categories
    Employee [] employee = new Employee[4];
    employee [0] = new Employee("Shinji Ikari", 15.50);     
    employee [1] = new Employee("Rei Ayanami", 21.35);  
    employee [2] = new Employee("Asuka Langley Soryu", 2.12);
    employee [3] = new Employee("Kaworu Nagisa", 20.01);
    
    //For Loop Running The showText Method From Employee.java For Each Employee In Array 
    try {
        for (Employee e : employee)     
            e.showText(hour);       
    } catch(ArrayIndexOutOfBoundsException oob) {
        System.out.println("NO");
    }   
}   

}

Employee.java

公开课员工{

//Sets Private Variables That Defines "Employee"
private String name;
private double wage;
private double totalpay;

    //Employee Constructors For Name And Wage
    public Employee(String employeename, double hourlypay) {
        name = employeename;
        wage = hourlypay;
    }   
    
    //Returns The Name Value        
    public String getName() {           
        return name;        
    }
    
    //Sets ASnd Updates Name
    public void setName(String ename) {         
        name = ename;   
    }       
    
    //Returns The Wage Value
    public double getWage() {
        return wage;
    }
    
    //Sets And Updates Wage
    public void setWage(double hourlypay) {
        wage = hourlypay;
    }
    
    //Method That Subtracts A 30% Tax From Base Hourly Pay  
    public double getTotalPay() {       
        totalpay = wage - (wage*0.3);
        return totalpay;
            }

    //Method Takes Name, Wage, and Totalpay From Employee.java Alongside Hour From HW3.java To Place Them In The Printed Statement
    public void showText(int hoursworked) {
        try {System.out.printf("The employee " +name+ ", who earns $%.2f per hour, worked for " +hoursworked+ " hours last week and took home $%.2f after taxes.\n", wage, getTotalPay()*hoursworked);
        } catch(ArrayIndexOutOfBoundsException oob) {
            System.out.println("Error");
        }
    }

}

在这个每周工资估算器程序中,我需要在从员工数组中提取员工时捕获可能的 ArrayIndexOutOfBoundsException。我不知道如何测试当前异常程序的放置是否正确,因为我不知道如何格式化每个循环以尝试循环超出我当前在主方法中拥有的四名员工。我当前的异常程序是否正确放置在循环中,还是需要将其放置在其他地方?

java arrays foreach catch-exception
1个回答
0
投票

如果未抛出异常,则无法捕获异常。 try / catch 语句 would catch

ArrayIndexOutOfBoundsException
如果它被抛出......但它没有被抛出。

try {
    System.out.printf("The employee " + name + 
         ", who earns $%.2f per hour, worked for " +
         hoursworked + 
         " hours last week and took home $%.2f after taxes.\n", 
         wage, getTotalPay() * hoursworked);
} catch (ArrayIndexOutOfBoundsException oob) {
    System.out.println("Error");
}

代码块内的代码根本不操作数组,因此无法抛出异常。

try {
    for (Employee e : employee)     
        e.showText(hour);       
} catch (ArrayIndexOutOfBoundsException oob) {
    System.out.println("NO");
}

这是对数组进行操作,但它的执行方式保证不会超出数组的范围。

如果你想导致那个异常......这样你就可以捕获它......你可以像这样编写循环

try {
    for (int i = 0; i < 9999; i++) // this loop is deliberately wrong    
        employee[i].showText(hour);       
} catch (ArrayIndexOutOfBoundsException oob) {
    System.out.println("NO");
}

现在当

i
4
时,你会得到一个异常。


代码正确吗?

是和否;见上文。

  • 是的,因为如果抛出异常,你会捕获该异常。这段代码确实可以编译。

  • 不,因为异常

    不会被抛出。有保证。

    尝试捕获无法抛出的(未经检查的)异常的代码是多余的。

© www.soinside.com 2019 - 2024. All rights reserved.