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 の伝播 (Throwable): throwable を RuntimeException にラップし、このメソッドを使用して例外が確実に配信され、RuntimeException 例外をスローします。

2.void propagateIfInstanceOf(Throwable, Class) throws X: のインスタンスの場合に限り、throwable を渡します

4.void propagateIfPossible(Throwable, Class) throws X: RuntimeException の場合に限り、throwable を渡しますおよび Error、または X のインスタンス。

[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.投げ可能なgetRootcause(投げ可能)

2.リストgetCausalChain(投げ可能)

3.ストリングGetStackTraceAsString(投げ可能) class 関連コンテンツの詳細については、PHP 中国語 Web サイト (www.php.cn) に注目してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!