1

有什麼辦法來壓縮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(); 
    } 

} 
+0

您可以使用Java 7的多緩存異常語法:http://www.oracle.com/technetwork/articles/java/java7exceptions-486908.html – adatapost

+0

謝謝!在這裏找到我的答案 – Pclef

回答

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

如果這是你壓縮的意思。

+0

是的!我正是這個意思。 try/catch塊內有一個try/catch塊是很奇怪的 – Pclef

1

您只能使用一個try塊,然後使用catch(Exception ex)來捕獲所有這些異常。如果您想對特定類型的異常做出反應,您必須對其進行測試。

1

可以做到這一點(見下文)。但是,你可能要考慮一下你的代碼的結構,例如,也許你可以重組,這樣你就不用調用input.nextLine在每個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"); 

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

所有的第一次,一個try塊可以跟隨一系列catch塊的:

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

這是不是最好的做法,因爲你可能要縮小try/catch塊處理有關例外

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

我也會看你的混合輸入與所有這些其他的東西askance。我會找到另一種方式。