我不明白为什么我在这行收到此错误:未报告的异常.....必须捕获或声明抛出
Vehicle v = new Vehicle("Opel",10,"HTG-454");
,当我把这个线在try/catch
,我通常不得到任何错误,但这次try/catch块不起作用。
public static void main(String[] args) {
Vehicle v = new Vehicle("Opel",10,"HTG-454");
Vector<Vehicle> vc =new Vector<Vehicle>();
vc.add(v);
Scanner sc = new Scanner(System.in);
boolean test=false;
while(!test)
try {
String name;
int i = 0;
int say;
int age;
String ID;
System.out.println("Araba Adeti Giriniz...");
say = Integer.parseInt(sc.nextLine());
for(i = 0; i<say; i++) {
System.out.println("Araba markası...");
name = sc.nextLine();
System.out.println("araba yası...");
age = Integer.parseInt(sc.nextLine());
System.out.println("araba modeli...");
ID = sc.nextLine();
test = true;
vc.add(new Vehicle(name, age, ID));
}
System.out.println(vc);
} catch (InvalidAgeException ex) {
test=false;
System.out.println("Hata Mesajı: " + ex.getMessage());
}
}
}
这是我在Vehicle类中的构造函数;
public Vehicle(String name, int age,String ID)throws InvalidAgeException{
this.name=name;
this.age=age;
this.ID=ID;
检查如果传递正确的价值观(即正确的数据类型)车辆的构造函数 - 它会帮助,如果你可以在这里发布构造函数定义 – Waqas
“但这次try/catch块不起作用..”向我们展示你是如何尝试捕获的 – Subin
我也有兴趣看看它是如何编译的没有相等数量的左/右花括号...;)(我的猜测是,你在'while(!test)'后面缺少一个'{')。 – brimborium