My Custom Exception is not thrown, instead the ArrayOutOfBoundsException Is thrown (JunitTest Error)

huangapple 未分类评论45阅读模式
英文:

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呢?

这是我的异常类:

  1. public class InvalidRowException extends ArrayIndexOutOfBoundsException {
  2. public InvalidRowException(int wrongRow){
  3. super("传递的行 " + wrongRow + " 不存在。");
  4. }
  5. }

这是我的方法:

  1. public void addItem(int row, int coll, Product product) throws InvalidRowException, InvalidColumnException {
  2. if (row < 0 || row > shelf.length)
  3. throw new InvalidRowException(row);
  4. if (coll < 0 || coll > shelf[1].length)
  5. throw new InvalidColumnException(coll);
  6. try {
  7. if (!(product instanceof Placeable)) {
  8. throw new ProductNotPlaceableException(product);
  9. } else if (shelf[row][coll] != null) {
  10. System.out.println("用序列号 " + shelf[row][coll].getSerialNumber()
  11. + " 的产品替换为序列号 " + product.getSerialNumber() + " 的产品");
  12. shelf[row][coll] = product;
  13. } else {
  14. shelf[row][coll] = product;
  15. }
  16. } catch (ProductNotPlaceableException e) {
  17. System.out.println(e.getMessage());
  18. }
  19. }
英文:

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,() -&gt; shelf.addItem(3, 0, bottle)); doesnt catch the ArrayIndexOutOfBoundsException but instead only my InvalidRowException?

This is my Exception Class:

  1. public class InvalidRowException extends ArrayIndexOutOfBoundsException {
  2. public InvalidRowException(int wrongRow){
  3. super(&quot;passed Row &quot; + wrongRow + &quot; doesnt excist.&quot;);
  4. }

}

This is my Method

  1. public void addItem(int row, int coll, Product product) throws InvalidRowException, InvalidColumnException {
  2. if (row &lt; 0 | row &gt; shelf.length)
  3. throw new InvalidRowException(row);
  4. if (coll &lt; 0 | coll &gt; shelf[1].length)
  5. throw new InvalidColumnException(coll);
  6. try {
  7. if (!(product instanceof Placeable)) {
  8. throw new ProductNotPlaceableException(product);
  9. } else if (shelf[row][coll] != null) {
  10. System.out.println(&quot;Replacing product with serial &quot; + shelf[row][coll].getSerialNumber()
  11. + &quot; by product with serial &quot; + product.getSerialNumber());
  12. shelf[row][coll] = product;
  13. } else {
  14. shelf[row][coll] = product;
  15. }
  16. } catch (ProductNotPlaceableException e) {
  17. System.out.println(e.getMessage());
  18. }
  19. }

答案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

huangapple
  • 本文由 发表于 2020年7月26日 12:51:45
  • 转载请务必保留本文链接:https://java.coder-hub.com/63096218.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定