18045010223
2025-07-07 0d3a683a0c97154b1f2e6657398664537e4e3e82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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> R ignore(Func<R, ?> 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 <E extends Throwable> void sneaky(Throwable e) throws E {
        throw (E) e;
    }
 
    @SuppressWarnings("unchecked")
    public static <R> R sneaky(Func<R, ?> f) {
        return ((Func<R, RuntimeException>) f).apply();
    }
 
    @SuppressWarnings("unchecked")
    public static void sneaky(Call<?> f) {
        ((Call<RuntimeException>) f).apply();
    }
 
    @FunctionalInterface
    public interface Func<R, E extends Throwable> {
        R apply() throws E;
    }
 
    @FunctionalInterface
    public interface Call<E extends Throwable> {
        void apply() throws E;
    }
}