有什麼辦法來壓縮try/catch代碼?現在,我的代碼在try/catch代碼中有一個try/catch代碼。Java壓縮Try/Catch代碼異常
if(petType.equals("DOG")) {
try {
String name = input.next();
String owner = input.next();
double weight = input.nextDouble();
SimpleDateFormat stdDate = new SimpleDateFormat("MM/dd/yy");
try {
Date vaccineDate = stdDate.parse(input.next());
boolean fixed = input.nextBoolean();
Dog x = new Dog(name,owner,weight,vaccineDate,fixed);
object.addPet(x);
}
catch (ParseException ex) {
System.out.println("ERROR - Vaccine date " + input.next() + " is not in mm/dd/yy format!");
input.nextLine();
}
}
catch(NoSuchElementException ex) {
System.out.println("ERROR - Missing fields. Skipping line " + lineNumber + "...");
input.nextLine();
}
}
您可以使用Java 7的多緩存異常語法:http://www.oracle.com/technetwork/articles/java/java7exceptions-486908.html – adatapost
謝謝!在這裏找到我的答案 – Pclef