All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dongliu.dbutils.internal.Exceptions Maven / Gradle / Ivy

package net.dongliu.dbutils.internal;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * Utils for exception
 *
 * @author Liu Dong {@literal }
 */
public class Exceptions {

    /**
     * Throw a exception with no need to declare throw clause in method signature
     */
    public static RuntimeException sneakyThrow(Throwable throwable) {
        Exceptions.magicThrow(throwable);
        return null;
    }

    @SuppressWarnings("unchecked")
    private static  void magicThrow(Throwable throwable) throws T {
        throw (T) throwable;
    }

    /**
     * Write exception stack trace to String
     */
    public static String toString(Throwable t) {
        try (StringWriter sw = new StringWriter();
             PrintWriter pw = new PrintWriter(sw)) {
            t.printStackTrace(pw);
            return sw.toString();
        } catch (IOException e) {
            throw Exceptions.sneakyThrow(e);
        }
    }

    /**
     * Write exception stack trace to string
     */
    public static String writeToString(Throwable e) {
        try (StringWriter sw = new StringWriter(256);
             PrintWriter pw = new PrintWriter(sw)) {
            e.printStackTrace(pw);
            return sw.toString();
        } catch (IOException e1) {
            //should not happen
            throw Exceptions.sneakyThrow(e1);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy