我不明白为什么我在这一行收到此错误:
Vehicle v = new Vehicle("Opel",10,"HTG-454");
当我把这条线放在 a 中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;