繁体   English   中英

Java 压缩 try/catch 代码异常

[英]Java compressing try/catch codes exceptions

有没有办法压缩 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();
  }

}

你可以这样做

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) {
...
}   

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

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

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

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

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

可以做到(见下文)。 但是你可能想考虑一下你的代码的结构,例如,也许你可以重组,这样你就不必在每个 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();
  }    
}

就个人而言,我不喜欢嵌套 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();
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM