Home >Java >javaTutorial >Java-Class Library-Guava-Throwables Class

Java-Class Library-Guava-Throwables Class

黄舟
黄舟Original
2017-01-19 13:25:391660browse

Sometimes, when we catch an exception, we pass the exception to the next try/catch block. Guava provides an exception handling utility class that can simply catch and rethrow multiple exceptions.

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

Convert checked exceptions into unchecked exceptions, for example:

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

Common methods of passing exceptions:

1.RuntimeException propagate(Throwable): wrap throwable into RuntimeException , use this method to ensure exception delivery and throw a RuntimeException

2.void propagateIfInstanceOf(Throwable, Class) throws X: If and only if it is an instance of X, pass throwable

 3.void propagateIfPossible(Throwable): If and only if it is a RuntimeException and Error, pass throwable

 4.void propagateIfPossible(Throwable, Class) throws X: If and only if it is a RuntimeException and Error, or an instance of X, pass 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’s exception chain handling method:

1.Throwable getRootCause(Throwable)

2.List getCausalChain(Throwable)

3.String getStackTraceAsString (Throwable)

The above is the content of Java-Class Library-Guava-Throwables class. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn