文章出處

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

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);
        }
    }    
}

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

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):把throwable包裝成RuntimeException,用該方法保證異常傳遞,拋出一個RuntimeException異常
  2.void propagateIfInstanceOf(Throwable, Class<X extends Exception>) throws X:當且僅當它是一個X的實例時,傳遞throwable
  3.void propagateIfPossible(Throwable):當且僅當它是一個RuntimeException和Error時,傳遞throwable
  4.void propagateIfPossible(Throwable, Class<X extends Throwable>) throws X:當且僅當它是一個RuntimeException和Error時,或者是一個X的實例時,傳遞throwable。

  使用實例:

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<Throwable> getCausalChain(Throwable)
  3.String getStackTraceAsString(Throwable)


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()