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

huangapple go评论78阅读模式
英文:

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,() -&gt; 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(&quot;passed Row &quot; + wrongRow + &quot; doesnt excist.&quot;);
}

}

This is my Method

public void addItem(int row, int coll, Product product) throws InvalidRowException, InvalidColumnException {
	if (row &lt; 0 | row &gt; shelf.length)
		throw new InvalidRowException(row);
	if (coll &lt; 0 | coll &gt; 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(&quot;Replacing product with serial &quot; + shelf[row][coll].getSerialNumber()
					+ &quot; by product with serial &quot; + 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

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

发表评论

匿名网友

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

确定