天天看點

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>

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