将IllegalArgumentException抛出到IDE中但未打印出来
来源:爱站网时间:2021-09-16编辑:网友分享
IllegalArgumentException在IDE中抛出,但没有打印出来,我如何添加打印行语句以获取抛出异常后在屏幕上打印出来的异常?抛出新的IllegalArgumentException(...
问题描述
IllegalArgumentException在IDE中抛出,但没有打印出来我如何添加打印行语句以获取抛出异常后将其打印到屏幕上的信息?
throw new IllegalArgumentException("Test scores must have a value less than 100 and greater than 0.");
package driver;
import java.util.Scanner;
public class TestScores
{
private double[] scoreArray;
public TestScores(double[] test) throws IllegalArgumentException
{
scoreArray = new double[test.length];
for (int i = 0; i 100)
throw new IllegalArgumentException("Test scores must have a value less than 100 and greater than 0.");
else
scoreArray[i] = test[i];
}
}
public double getAverage()
{
double total = 0.0;
for (int i = 0; i
思路一:
您的构造函数可能会抛出此异常,因此您应该像这样使用它
TestScores testScore;
try {
testScore = new TestScores(scoreArray);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
思路二:
IllegalArgumentException扩展了RuntimeException,因此在正常情况下,您的Compiler / IDE不会强迫您捕获它或将其扔回到调用方。如果您确实决定捕获它,则可以这样在记录器中将其打印出来:
log.error(e.getMessage(), e);