首頁 > Java > java教程 > 主體

Java-類別庫-Guava-Throwables類

黄舟
發布: 2017-01-19 13:25:39
原創
1610 人瀏覽過

有時候, 當我們我們捕獲異常, 並且像把這個異常傳遞到下一個try/catch塊中。 Guava提供了一個異常處理工具類, 可以簡單地捕獲和重新拋出多個異常。

[code]import java.io.IOException;
import org.junit.Test;
import com.google.common.base.Throwables;

public class ThrowablesTest {

    @Test
    public void testThrowables(){
        try {
            throw new Exception();
        } catch (Throwable t) {
            String ss = Throwables.getStackTraceAsString(t);
            System.out.println("ss:"+ss);
            Throwables.propagate(t);
        }
    }

    @Test
    public void call() throws IOException {
        try {
            throw new IOException();
        } catch (Throwable t) {
            Throwables.propagateIfInstanceOf(t, IOException.class);
            throw Throwables.propagate(t);
        }
    }    
}
登入後複製

將檢查異常轉換成未檢查異常,例如:

[code]import java.io.InputStream;
import java.net.URL;
import org.junit.Test;
import com.google.common.base.Throwables;

public class ThrowablesTest {

    @Test
    public void testCheckException(){
        try {  
            URL url = new URL("http://ociweb.com");  
            final InputStream in = url.openStream();  
            // read from the input stream  
            in.close();  
        } catch (Throwable t) {  
            throw Throwables.propagate(t);  
        }  
    }
}
登入後複製

 傳遞異常的常用方法:

  1.RuntimeException propagate(Throwable):把throwableceptionRuntimeException,用該方法保證一個異常傳遞異常值的異常 

  2.void propagateIfInstanceOf(Throwable, Class) throws X:當且僅當它是一個X的實例時,傳遞throwable 

  3.void propaic prodid propaic(Thwulwulwulwulwulwulwet Nelisconpao​​did propaidable(Thr. ,傳遞throwable 

  4.void propagateIfPossible(Throwable, Class) throws X:當且僅當它是一個RuntimeException和Error時,或者是一個X的實例時,傳遞throwable。

[code]import java.io.IOException;
import org.junit.Test;
import com.google.common.base.Throwables;

public class ThrowablesTest {    
    @Test
    public void testThrowables(){
        try {
            throw new Exception();
        } catch (Throwable t) {
            String ss = Throwables.getStackTraceAsString(t);
            System.out.println("ss:"+ss);
            Throwables.propagate(t);
        }
    }

    @Test
    public void call() throws IOException {
        try {
            throw new IOException();
        } catch (Throwable t) {
            Throwables.propagateIfInstanceOf(t, IOException.class);
            throw Throwables.propagate(t);
        }
    }

    public Void testPropagateIfPossible() throws Exception {
          try {
              throw new Exception();
          } catch (Throwable t) {
            Throwables.propagateIfPossible(t, Exception.class);
            Throwables.propagate(t);
          }

          return null;
    }
}
登入後複製

 Guava的異常鏈處理方法:

  1.Throwable getRootCause(Throwable) 

  2.List getCausalChain(Throwable)以上就是Java-類別庫-Guava-Throwables類的內容,更多相關內容請關注PHP中文網(www.php.cn)!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!