gpt4 book ai didi

Java 程序不会抛出所需的异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:05:21 26 4
gpt4 key购买 nike

我正在尝试用 Java 开发卡片验证程序。 isCardValid 方法基本上检查给定的卡号是否有效。我在那里指定了一个应该抛出 IllegalArgumentException 的条件,但它没有抛出并且程序以 exit code 0

成功执行

以下是我的 Java 文件:-

public class CardValidation {

public static String getCardBrand(final String cardIsin){


String brand = null;

if (cardIsin == null || cardIsin.equals("")){
log.warn("Given card BIN is null!! So returning null");
return brand;
}


if (cardIsin.substring(0,2).matches("51|52|53|54|55"))
brand = "MasterCard".toUpperCase();
else if (cardIsin.startsWith("4"))
brand = "Visa".toUpperCase();
else if (cardIsin.startsWith("34") || cardIsin.startsWith("37"))
brand = "Amex".toUpperCase();
else if (Pattern.matches(maestroPattern,cardIsin))
brand = "Maestro".toUpperCase();
else if (Pattern.matches(diners, cardIsin))
brand = "Diners".toUpperCase();
else if (Pattern.matches(discover,cardIsin))
brand = "Discover".toUpperCase();
else if (doesFallInRupayRange(cardIsin))
brand = "Rupay".toUpperCase();
else if (cardIsin.startsWith("35"))
brand = "JCB";

return brand;
}

public static boolean isCardValid(final String cardNumber){

if(cardNumber.matches("[^0-9]"))
throw new IllegalArgumentException("INVALID ARGUMENT!!!! Card number should contain ONLY digits.");

String cardBrand = getCardBrand(cardNumber);

if (cardBrand == null)
return false;
if(cardBrand.equalsIgnoreCase("VISA") || cardBrand.equalsIgnoreCase("MasterCard") ||
cardBrand.equalsIgnoreCase("DISCOVER") || cardBrand.equalsIgnoreCase("RUPAY")) {
return cardNumber.length() == 16;
}
else if(cardBrand.equalsIgnoreCase("AMEX")) {
return cardNumber.length() == 15;
}
else if(cardBrand.equalsIgnoreCase("MAESTRO")) {
return cardNumber.length() == 16 || cardNumber.length() == 19;
}
else if(cardBrand.equalsIgnoreCase("DINERS")) {
return cardNumber.length() == 14;
}
log.info("Unknown card number brand: " + cardNumber);
return cardNumber.length() >= 12;
}

public static boolean doesFallInRupayRange(final String isin){

long intIsin = Long.parseLong(isin.replaceAll("[^\\d]", "").substring(0,6));

for (int row = 0; row < CardValidation.rupayRange.length; row++)
{
try {
if (intIsin >= CardValidation.rupayRange[row][0] && intIsin <= CardValidation.rupayRange[row][1])
return true;
}
catch (Exception e) {
log.error("Error while checking for RuPay: " + isin, e);
}
}
return false;
}

public static void main (String... args){
CardValidation.isCardValid("42424242hghghghgh");
}
}

现在下面的代码片段应该抛出提到的 Exception,不是吗,因为我故意从我的 ma​​in 向方法提供无效参数?

 if(cardNumber.matches("[^0-9]"))
throw new IllegalArgumentException("INVALID ARGUMENT!!!! Card number should contain ONLY digits.");

以下是我在控制台中得到的输出

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

Process finished with exit code 0

最佳答案

您的正则表达式错误

试试这个:

String regex = "\d+";

或者:

String regex = "[0-9]+";

因此不匹配无效号码;因此也不异常(exception)。

为了使用上面的模式,你必须颠倒逻辑:

if ( ! input.matches(regex) ) {
throw exception

但请注意:您的代码“不好”。例如:您没有使用任何类型的抽象。您将信用卡类型表示为字符串。这就像 super 糟糕。因为处理不同信用卡的每一段代码......都必须知道“AMEX”的意思是“美国运通”等等。考虑使用 Enum 代替。

换句话说:您真的很想阅读/学习如何做好面向对象的设计!

关于Java 程序不会抛出所需的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38912962/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com