1

有什么方法可以压缩 try/catch 块代码吗?现在,我的代码在 try/catch 代码中有一个 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();
  }

}
4

5 回答 5

5

你可以这样做

if(petType.equals("DOG")) {

  try {
    String name = input.next();
    String owner = input.next();
    double weight = input.nextDouble();
    SimpleDateFormat stdDate = new SimpleDateFormat("MM/dd/yy");
    Date vaccineDate = stdDate.parse(input.next());
    boolean fixed = input.nextBoolean();
    Dog x = new Dog(name,owner,weight,vaccineDate,fixed);
    object.addPet(x);
  }
  catch(NoSuchElementException ex) {
    System.out.println("ERROR - Missing fields. Skipping line " + lineNumber + "...");
    input.nextLine();
  }
  catch (ParseException ex) {
    System.out.println("ERROR - Vaccine date " + input.next() + " is not in mm/dd/yy format!");
    input.nextLine();
  }
}

或使用 Java 7

try {
...
} catch(ParseException | NoSuchElementException ex) {
...
}   

如果这就是你所说的压缩。

于 2012-11-09T01:38:48.290 回答
3

首先,一个try块后面可以跟一系列catch块:

try {
    throw IOException("msg");
    ...
    throw InterruptedException("msg");
}
catch (IOException ioe){
    ...
 } catch (InterruptedException ie) {
    ...
 }

这不是最佳实践,因为您可能希望缩小 try/catch 块以处理有关异常的较小代码内容

于 2012-11-09T01:42:16.253 回答
1

您只能使用一个 try 块,然后使用 catch(Exception ex) 捕获所有这些异常。如果您想对特定类型的异常做出反应,则必须对其进行测试。

于 2012-11-09T01:40:31.963 回答
1

可以做到(见下文)。但是您可能想考虑一下代码的结构,例如,也许您可​​以进行重组,这样您就不必在每个 catch 块中调用 input.nextLine。

if(petType.equals("DOG")) {

  try {
    String name = input.next();
    String owner = input.next();
    double weight = input.nextDouble();
    SimpleDateFormat stdDate = new SimpleDateFormat("MM/dd/yy");

    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();
  }    
}
于 2012-11-09T01:41:21.073 回答
1

就个人而言,我不喜欢嵌套 try/catch 块。我不会这样写;我更喜欢这样:

if(petType.equals("DOG")) {

  String vaccineDateString;
  try {
      String name = input.next();
      String owner = input.next();
      double weight = input.nextDouble();
      DateFormat stdDate = new SimpleDateFormat("MM/dd/yy");
      stdDate.setLenient(false);
      vaccineDateString = input.next();
      Date vaccineDate = stdDate.parse(vaccineDateString);
      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 " + vaccineDateString + " is not in MM/dd/yy format!");
        input.nextLine();
    } catch(NoSuchElementException ex) {
        System.out.println("ERROR - Missing fields. Skipping line " + lineNumber + "...");
        input.nextLine();
    }
}

我也会怀疑您将输入与所有其他内容混合在一起。我会找到另一种方法。

于 2012-11-09T01:44:09.473 回答