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)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use