天天看點

Guava的異常工具類--Throwables

Guava為我們提供了一個非常友善并且實用的異常處理工具類:Throwables類。

下面是本人的一些簡要總結:

我們在日常的開發中遇到異常的時候,往往需要做下面的幾件事情中的一些:

1. 将異常資訊存入資料庫、日志檔案、或者郵件等。

2. 将受檢查的異常轉換為運作時異常

3. 在代碼中得到引發異常的最低層異常

4. 得到異常鍊

5. 過濾異常,隻抛出感興趣的異常

而借助于Guava的Throwables,我們可以很友善的做到這些:

以list的方式得到throwable的異常鍊:

1

<code>static</code> <code>List&lt;Throwable&gt; getCausalChain(Throwable throwable)</code>

  

傳回最底層的異常

<code>static</code> <code>Throwable getRootCause(Throwable throwable)</code>

傳回printStackTrace的結果string

<code>static</code> <code>String getStackTraceAsString(Throwable throwable)</code>

把受檢查的異常轉換為運作時異常:

<code>public</code> <code>static</code> <code>RuntimeException propagate(Throwable throwable)</code>

隻抛出感興趣的異常:

2

<code>public</code> <code>static</code> <code>&lt;X </code><code>extends</code> <code>Throwable&gt; </code><code>void</code> <code>propagateIfInstanceOf(</code>

<code>      </code><code>@Nullable</code> <code>Throwable throwable, Class&lt;X&gt; declaredType) </code><code>throws</code> <code>X</code>

下面用官網的例子說大緻說一下吧:

3

4

5

6

7

8

9

<code>try</code> <code>{</code>

<code>  </code><code>someMethodThatCouldThrowAnything();</code>

<code>} </code><code>catch</code> <code>(IKnowWhatToDoWithThisException e) {</code>

<code>  </code><code>handle(e);</code>

<code>} </code><code>catch</code> <code>(Throwable t) {</code>

<code>  </code><code>Throwables.propagateIfInstanceOf(t, IOException.</code><code>class</code><code>);</code>

<code>  </code><code>Throwables.propagateIfInstanceOf(t, SQLException.</code><code>class</code><code>);</code>

<code>  </code><code>throw</code> <code>Throwables.propagate(t);</code>

<code>}</code>

  上面的例子中,隻抛出感興趣的IOException或者SQLException,至于其他的異常直接轉換為運作時異常。

<code>    </code><code>someMethodThatCouldThrowAnything();</code>

<code>  </code><code>} </code><code>catch</code> <code>(IKnowWhatToDoWithThisException e) {</code>

<code>    </code><code>handle(e);</code>

<code>  </code><code>} </code><code>catch</code> <code>(Throwable t) {</code>

<code>    </code><code>Throwables.propagateIfPossible(t);</code>

<code>    </code><code>throw</code> <code>new</code> <code>RuntimeException(</code><code>"unexpected"</code><code>, t);</code>

<code>  </code><code>}</code>

  propagateIfPossible方法隻會對RuntimeException或者Error異常感興趣

<code>     </code><code>someMethodThatCouldThrowAnything();</code>

<code>   </code><code>} </code><code>catch</code> <code>(IKnowWhatToDoWithThisException e) {</code>

<code>     </code><code>handle(e);</code>

<code>   </code><code>} </code><code>catch</code> <code>(Throwable t) {</code>

<code>     </code><code>Throwables.propagateIfPossible(t, OtherException.</code><code>class</code><code>);</code>

<code>     </code><code>throw</code> <code>new</code> <code>RuntimeException(</code><code>"unexpected"</code><code>, t);</code>

<code>   </code><code>}</code>

  propagateIfPossible的另外一個重載方法,容許我們來自己指定一個感興趣的異常。這樣這個方法隻會對RuntimeException或者Error和我們指定的這3種異常感興趣。

<code>T doSomething() {</code>

<code>     </code><code>try</code> <code>{</code>

<code>       </code><code>return</code> <code>someMethodThatCouldThrowAnything();</code>

<code>     </code><code>} </code><code>catch</code> <code>(IKnowWhatToDoWithThisException e) {</code>

<code>       </code><code>return</code> <code>handle(e);</code>

<code>     </code><code>} </code><code>catch</code> <code>(Throwable t) {</code>

<code>       </code><code>throw</code> <code>Throwables.propagate(t);</code>

<code>     </code><code>}</code>

  上面的方法會将受檢查的異常轉換為運作時異常。

==============================================================================

本文轉自被遺忘的部落格園部落格,原文連結:http://www.cnblogs.com/rollenholt/p/3527645.html,如需轉載請自行聯系原作者