Maison > Java > javaDidacticiel > le corps du texte

Classe Java-Class Library-Guava-Throwables

黄舟
Libérer: 2017-01-19 13:25:39
original
1611 Les gens l'ont consulté

Parfois, lorsque nous captons une exception, nous transmettons l'exception au prochain bloc try/catch. Guava fournit une classe utilitaire de gestion des exceptions qui peut simplement intercepter et relancer plusieurs 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);
        }
    }    
}
Copier après la connexion

Convertissez les exceptions vérifiées en exceptions non vérifiées, par exemple :

[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);  
        }  
    }
}
Copier après la connexion

Méthodes courantes de transmission des exceptions :

1. propagation de RuntimeException (Throwable) : throwable est emballé sous le nom de RuntimeException, utilisez cette méthode pour garantir la livraison des exceptions et lancez une RuntimeException

2.void propagateIfInstanceOf(Throwable, Class) lance X : si et seulement s'il s'agit d'une instance de X, transmettez throwable

 3.void propagateIfPossible(Throwable) : Si et seulement s'il s'agit d'une RuntimeException et d'une erreur, transmettez throwable

 4.void propagateIfPossible(Throwable, Class) lance X : Si et seulement si c'est Quand c'est le cas une RuntimeException et une erreur, ou lorsqu'il s'agit d'une instance de X, passez une valeur jetable.

[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;
    }
}
Copier après la connexion

Méthode de gestion de la chaîne d'exceptions de Guava :

1. Throwable getRootCause(Throwable)

2. List getCausalChain(Throwable)

3. String getStackTraceAsString(Throwable)

Ce qui précède est le contenu de la classe Java-Class Library-Guava-Throwables. Pour plus de contenu connexe, veuillez faire attention au site Web PHP chinois (www.php.cn) !


Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!