简体   繁体   English

Java 压缩 try/catch 代码异常

[英]Java compressing try/catch codes exceptions

Is there any way to compress the try/catch block codes?有没有办法压缩 try/catch 块代码? Right now, my code has a try/catch code inside a try/catch code.现在,我的代码在 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();
  }

}

You could do this你可以这样做

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();
  }
}

Or with Java 7或者使用 Java 7

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

If that's what you meant by compression.如果这就是您所说的压缩。

1st of all, a single try block can be followed by a series of catch blocks:首先,一个try块后面可以跟一系列catch块:

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

This is not the best practice, because you might want to narrow your try/catch blocks to handle smaller content of code regarding the Exceptions这不是最佳实践,因为您可能希望缩小 try/catch 块的范围以处理与异常相关的较小代码内容

You can only use one try block, and then use catch(Exception ex) to catch all those exceptions.您只能使用一个 try 块,然后使用 catch(Exception ex) 来捕获所有这些异常。 If you want to react to the specific kind of exception, you have to test for it.如果您想对特定类型的异常做出反应,则必须对其进行测试。

You can do it (see below).可以做到(见下文)。 But you might want to think about the structure of your code, for example, maybe you can restructure so that you don't have to call input.nextLine in each catch block.但是你可能想考虑一下你的代码的结构,例如,也许你可以重组,这样你就不必在每个 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();
  }    
}

Personally, I don't like nesting try/catch blocks.就个人而言,我不喜欢嵌套 try/catch 块。 I wouldn't write it this way;我不会这样写; I'd prefer it more like this:我更喜欢这样:

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();
    }
}

I would also look askance at your mingling input with all this other stuff.我也会怀疑您将输入与所有其他内容混合在一起。 I'd find another way.我会找到另一种方式。

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

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