2011-09-27 68 views
0
 //Assume list1 and list2 are populated with states as 2 
    foo (List<Class1> list1, List <Class1> list2) { 

     boolean error = false; 
      try { 
      operate on list1 
     } catch (Exception e) { 
       error = true; 
      //Modify list1 objects to state 1 
     } 

      try { 
      operate on list2 
     } catch (Exception e) { 
       error = true; 
      //Modify list2 objects to state 1 
     } 

     //What are the problems and what is the best practice for this 
     if (error) 
      throw new Exception(…); //How should i really rethrow the exception 

    } 
+0

設計是否合理?如果第一個操作已經出現錯誤,並且只打算拋出異常,那麼在list2上操作的意義何在?爲什麼不馬上扔? –

回答

0

技術上沒有什麼不對的地方,比獲得throws條款正確,等你甚至可以保存捕獲的異常之一,並重新拋出其他。

不清楚你爲什麼想要這樣做,但我做了很多事。

2

我所做的主要改進是存儲所有發生的異常,並在某處以某種方式使它們可用。否則,它似乎很好。

對於持懷疑態度的人來說,即使發生異常時也想完成一些工作並不是真的不正常。批處理是一個非常典型的例子。