package org.yzh.commons.util; import java.io.PrintWriter; import java.io.StringWriter; public class Exceptions { public static String getStackTrace(Throwable error) { if (error == null) return null; StringWriter stackTrace = new StringWriter(7680); error.printStackTrace(new PrintWriter(stackTrace, true)); stackTrace.flush(); return stackTrace.toString(); } public static R ignore(Func f) { try { return f.apply(); } catch (Throwable ignored) { return null; } } public static void ignore(Call f) { try { f.apply(); } catch (Throwable ignored) { } } @SuppressWarnings("unchecked") public static void sneaky(Throwable e) throws E { throw (E) e; } @SuppressWarnings("unchecked") public static R sneaky(Func f) { return ((Func) f).apply(); } @SuppressWarnings("unchecked") public static void sneaky(Call f) { ((Call) f).apply(); } @FunctionalInterface public interface Func { R apply() throws E; } @FunctionalInterface public interface Call { void apply() throws E; } }