
Guava - Useful Classes
- Guava - Optional Class
- Guava - Preconditions Class
- Guava - Ordering Class
- Guava - Objects Class
- Guava - Range Class
- Guava - Throwables Class
- Guava - LoadingCache Interface
Guava - Collection Utilities
- Guava - Collections Utilities
- Guava - MultiSet Interface
- Guava - MultiMap Interface
- Guava - BiMap Interface
- Guava - Table Interface
Guava - String Utilities
- Guava - String Utilities
- Guava - Joiner class
- Guava - Splitter
- Guava - CharMatcher
- Guava - CaseFormat
Guava - Primitive Utilities
- Guava - Primitive Utilities
- Guava - Bytes
- Guava - Shorts
- Guava - Ints
- Guava - Longs
- Guava - Floats
- Guava - Doubles
- Guava - Chars
- Guava - Booleans
Guava - Math Utilities
Guava - Useful Resources
Guava - Throwables Class
Throwables class provides utility methods related to Throwable interface.
Class Declaration
Following is the declaration for com.google.common.base.Throwables class −
public final class Throwables extends Object
Sr.No | Method & Description |
---|---|
1 |
static List<Throwable> getCausalChain(Throwable throwable) Gets a Throwable cause chain as a list. |
2 |
static Throwable getRootCause(Throwable throwable) Returns the innermost cause of throwable. |
3 |
static String getStackTraceAsString(Throwable throwable) Returns a string containing the result of toString(), followed by the full, recursive stack trace of throwable. |
4 |
static RuntimeException propagate(Throwable throwable) Propagates throwable as-is if it is an instance of RuntimeException or Error, or else as a last resort, wraps it in a RuntimeException then propagates. |
5 |
static <X extends Throwable> void propagateIfInstanceOf(Throwable throwable, Class<X> declaredType) Propagates throwable exactly as-is, if and only if it is an instance of declaredType. |
6 |
static void propagateIfPossible(Throwable throwable) Propagates throwable exactly as-is, if and only if it is an instance of RuntimeException or Error. |
7 |
static <X extends Throwable> void propagateIfPossible(Throwable throwable, Class<X> declaredType) Propagates throwable exactly as-is, if and only if it is an instance of RuntimeException, Error, or declaredType. |
8 |
static <X1 extends Throwable,X2 extends Throwable>void propagateIfPossible(Throwable throwable, Class<X1> declaredType1, Class<X2> declaredType2) Propagates throwable exactly as-is, if and only if it is an instance of RuntimeException, Error, declaredType1, or declaredType2. |
Methods Inherited
This class inherits methods from the following class −
- java.lang.Object
Example - Getting Root Cause of Exception
GuavaTester.java
package com.tutorialspoint; import java.io.IOException; import com.google.common.base.Objects; import com.google.common.base.Throwables; public class GuavaTester { public static void main(String args[]) { GuavaTester tester = new GuavaTester(); try { tester.showcaseThrowables(); } catch (InvalidInputException e) { //get the root cause System.out.println(Throwables.getRootCause(e)); } catch (Exception e) { //get the stack trace in string format System.out.println(Throwables.getStackTraceAsString(e)); } } public void showcaseThrowables() throws InvalidInputException { try { sqrt(-3.0); } catch (Throwable e) { //check the type of exception and throw it Throwables.propagateIfInstanceOf(e, InvalidInputException.class); Throwables.propagate(e); } } public double sqrt(double input) throws InvalidInputException { if(input < 0) throw new InvalidInputException(); return Math.sqrt(input); } } class InvalidInputException extends Exception { }
Output
Run the GuavaTester and verify the output −
com.tutorialspoint.InvalidInputException
Example - Propagating Exception
GuavaTester.java
package com.tutorialspoint; import java.io.IOException; import com.google.common.base.Objects; import com.google.common.base.Throwables; public class GuavaTester { public static void main(String args[]) { GuavaTester tester = new GuavaTester(); try { tester.showcaseThrowables(); } catch (Exception e) { System.out.println(Throwables.getStackTraceAsString(e)); } } public void showcaseThrowables() { try { int[] data = {1,2,3}; getValue(data, 4); } catch (Throwable e) { Throwables.propagateIfInstanceOf(e, IndexOutOfBoundsException.class); Throwables.propagate(e); } } public double getValue(int[] list, int index) throws IndexOutOfBoundsException { return list[index]; } } class InvalidInputException extends Exception { }
Output
Run the GuavaTester and verify the output −
java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 3 at com.tutorialspoint.GuavaTester.getValue(GuavaTester.java:58) at com.tutorialspoint.GuavaTester.showcaseThrowables1(GuavaTester.java:45) at com.tutorialspoint.GuavaTester.main(GuavaTester.java:25)