2016-04-30 1192 views
1

請看下面的代碼的新列表:捕捉多個異常並拋出異常

public void editProduct(String articleNumber, ...) { 
product.setArticleNumber(articleNumber); 
product.setDescription(description); 
product.setLendable(lendable); 
product.setName(name); 
product.setPrice(price); 
product.setPlace(place); 
} 


所有制定者可以拋出一個自定義消息ProductException。 但我想抓住所有的例外。如果有例外情況,我想向所有錯誤的GUI發送一個列表。

我怎麼能做到這一點,或者我需要圍繞一個嘗試捕捉每一行?

+0

如果發生異常,爲什麼要繼續嘗試更改產品?如果發生異常,則表示存在問題;並且'product'應該保留在開始編輯它之前的狀態(* Effective Java *指這是[「failure atomicity」](http://stackoverflow.com/questions/29842845/what-是故障影響的原子使用的逐J-布洛赫和 - 如何-其益合-的組術語ⅰ))。 –

+0

您好,我想向用戶顯示不同的錯誤:「 - 沒有描述, - 沒有價格 - ......」 – Demian

+0

什麼是例外這裏爲你的產品是空的一些方法參數?爲空?這些方法都是簡單的setter?這裏怎麼可能會出現異常... – GOXR3PLUS

回答

0

,如果您需要在列表中的所有例外,那麼你必須圍繞通過嘗試捕捉每一行,並添加例外列表中,並在最後的檢查列表的大小大於0,則返回一個列表作爲例外。

List<CustomException> exceptionList = new ArrayList<CustomException>(); 
public void editProduct(String articleNumber, ...) 
{ 
    try{ 
      product.setArticleNumber(articleNumber); 
    }catch(CustomException exception) 
    { 
     exceptionList.add(exception); 
     } 
     try{ 
     product.setDescription(description); 
     }catch(CustomException exception) 
    { 
     exceptionList.add(exception); 
     } 
    try{ 
    product.setLendable(lendable); 
    }catch(CustomException exception) 
    { 
     exceptionList.add(exception); 
     } 
    try{ 
      product.setName(name); 
    }catch(CustomException exception) 
    { 
     exceptionList.add(exception); 
     } 
     try{ 
     product.setPrice(price); 
    }catch(CustomException exception) 
    { 
     exceptionList.add(exception); 
     } 
    try{ 
     product.setPlace(place); 
    }catch(CustomException exception) 
    { 
     exceptionList.add(exception); 
     } 
} 
+0

謝謝你,但有沒有這個更好的解決方案? – Demian

+0

而不是從setter方法拋出異常,你可以添加一個驗證方法來處理你的所有setter檢查,並在那裏執行你所有的操作validations.that將是連擊方法。 –

+0

嗨,這是一種可能性。但是我想保留domainclass中的所有「域」代碼。 – Demian

0

如果你真的想要做這樣的事情,你可以委託屬性設置爲會趕上運行二傳手作爲拉姆達的方法,再搭上可能是個例外,或將其添加到列表:

​​
1

喲可以嘗試下面的代碼。如果合適

public void editProduct(String articleNumber, ...) { 
    int count=1; 
    Vector<String> v=new Vector<String>(); 
    while(count<=6) 
    { 
     try{ 

      switch(count) 
      { 
       case 1:product.setArticleNumber(articleNumber);break;//assume it will throw ArticalException 
       case 2:product.setDescription(description);break; //DescriptionException 
       case 3:product.setLendable(lendable);break;   //LendableException 
       case 4:product.setName(name);break;     //NameException 
       case 5:product.setPrice(price);break;    //PriceException 
       case 6:product.setPlace(place);break;    //PlaceException 
      } 
      count++; 
     }catch(Exception e) 
     { 
      v.add(e.getMessage); 
      count++; 
      /* 
      *suppose there is some exception like ArticalException,PriceException 
      *then it will store in vector and your program running continue 
      *and at last you have the list of all exceptions that are occured in program 
      *now you will take desired action what you want to do 
      **/ 
     } 
    } 

} 
+0

嗨,我喜歡你的解決方案!我不知道這是最佳做法 一件小事,如果二傳手失敗了,計數仍然是一樣的。所以你有一個無限循環。 也許你可以在開關之前或最後一組中增加? – Demian

+0

看我代碼..有一個計數++ statement.there爲count ++語句之後開關和catch塊 –

+0

是啊...但'爲(計數= 1;計數<= 6;計數++)'做同樣的事情,並且它更可讀。此外,你應該趕上'ProductException'。捕捉「Exception」是一個糟糕的主意,因爲它會導致意外的異常作爲驗證錯誤報告給用戶。 –