我试图测试两个字符串的大小,并在try-catch块中捕获错误。我在包含throw语句的行中收到“找不到符号”错误。
class test {
public static void main(String args[]) {
String a = "abcdefghi";
String b = "abcdefgh";
try {
if (a.length() != 9) {
throw IllegalArgumentException;
}
if (b.length() != 10) {
throw IllegalArgumentException;
}
} catch (IllegalArgumentException e) {
System.out.println(e);
}
}
}
答案 0 :(得分:-1)
您需要创建抛出异常的实例
public static void main(String args[]){
String a = "abcdefghi";
String b = "abcdefgh";
try{
if(a.length()!=9){
throw new IllegalArgumentException();
}
if(b.length()!=10){
throw new IllegalArgumentException();
}
}
catch(IllegalArgumentException e){
System.out.println(e);
}
}