英文:
My Custom Exception is not thrown, instead the ArrayOutOfBoundsException Is thrown (JunitTest Error)
问题
以下是翻译好的内容:
我在自定义异常方面遇到了问题,我想在输入的行/列在我的shelf[][]中不存在时抛出自定义异常,这似乎有点奏效。当我编译主程序时,自定义异常确实会被抛出(错误消息会被打印出来)- 尽管我的代码中的抛出部分显然从未被执行过(https://i.stack.imgur.com/1OY52.png)- 但它也会抛出ArrayIndexOutOfBoundsException。因此,当我为抛出InvalidRow/ColumnException的方法进行Junit测试时,它会失败,因为它抛出了ArrayOutOfBoundsException。
我如何解决这个问题,使得我的Junit测试assertThrows(InvalidRowException.class,() -> shelf.addItem(3, 0, bottle));
只捕获InvalidRowException,而不是ArrayIndexOutOfBoundsException呢?
这是我的异常类:
public class InvalidRowException extends ArrayIndexOutOfBoundsException {
public InvalidRowException(int wrongRow){
super("传递的行 " + wrongRow + " 不存在。");
}
}
这是我的方法:
public void addItem(int row, int coll, Product product) throws InvalidRowException, InvalidColumnException {
if (row < 0 || row > shelf.length)
throw new InvalidRowException(row);
if (coll < 0 || coll > shelf[1].length)
throw new InvalidColumnException(coll);
try {
if (!(product instanceof Placeable)) {
throw new ProductNotPlaceableException(product);
} else if (shelf[row][coll] != null) {
System.out.println("用序列号 " + shelf[row][coll].getSerialNumber()
+ " 的产品替换为序列号 " + product.getSerialNumber() + " 的产品");
shelf[row][coll] = product;
} else {
shelf[row][coll] = product;
}
} catch (ProductNotPlaceableException e) {
System.out.println(e.getMessage());
}
}
英文:
i have a Problem with my Custom Exception, i want to throw a Custom Exception when the entered row/col does not excist in my shelf[][] which only kind of works. The custom exception does get thrown when i compile my main (error message is printed)- even though the throw part in my Code is apparently never reached (https://i.stack.imgur.com/1OY52.png) - but it also throws the ArrayIndexOutOfBoundsException. So when i Junit test my method for throwing InvalidRow/ColumnException it fails because it throws the ArrayOutOfBoundsException.
How do i solve this problem so my Junit test assertThrows(InvalidRowException.class,() -> shelf.addItem(3, 0, bottle));
doesnt catch the ArrayIndexOutOfBoundsException but instead only my InvalidRowException?
This is my Exception Class:
public class InvalidRowException extends ArrayIndexOutOfBoundsException {
public InvalidRowException(int wrongRow){
super("passed Row " + wrongRow + " doesnt excist.");
}
}
This is my Method
public void addItem(int row, int coll, Product product) throws InvalidRowException, InvalidColumnException {
if (row < 0 | row > shelf.length)
throw new InvalidRowException(row);
if (coll < 0 | coll > shelf[1].length)
throw new InvalidColumnException(coll);
try {
if (!(product instanceof Placeable)) {
throw new ProductNotPlaceableException(product);
} else if (shelf[row][coll] != null) {
System.out.println("Replacing product with serial " + shelf[row][coll].getSerialNumber()
+ " by product with serial " + product.getSerialNumber());
shelf[row][coll] = product;
} else {
shelf[row][coll] = product;
}
} catch (ProductNotPlaceableException e) {
System.out.println(e.getMessage());
}
}
答案1
得分: 1
抛出异常,当行数大于 shelf.length 时
你应该检查行数是否大于 shelf.length - 1,因为数组是从 0 开始计数的
类似地,对于 coll,正确的检查是 coll 大于 shelf[row].length - 1
英文:
You throw exception for
row > shelf.length
You should check for row > shelf.length -1 as arrays are 0 based
Similarly for coll the correct check is coll> shelf[row].length-1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论