Example usage for java.lang Throwable Throwable

List of usage examples for java.lang Throwable Throwable

Introduction

In this page you can find the example usage for java.lang Throwable Throwable.

Prototype

public Throwable() 

Source Link

Document

Constructs a new throwable with null as its detail message.

Usage

From source file:Main.java

/**
 * Like {@link #sprintFullStack(Throwable)}, renders a stacktrace as a String for logging,
 * but excludes stack trace elements common with the calling scope, which may
 * be considered irrelevant to application functionality testing
 * @param t an exception/*from ww  w.  j a  v a  2s .  com*/
 * @return a string representation of a shorter stack trace
 */
public static String sprintShortStack(final Throwable t) {
    if (t == null) {
        return "";
    }

    StringWriter writer = new StringWriter();
    Throwable local = new Throwable();

    StackTraceElement[] localStack = local.getStackTrace();
    StackTraceElement[] tStack = t.getStackTrace();

    int m = tStack.length - 1, n = localStack.length - 1;
    while (m >= 0 && n >= 0 && tStack[m].equals(localStack[n])) {
        m--;
        n--;
    }

    int framesInCommon = tStack.length - 1 - (m + 1);

    t.setStackTrace(Arrays.copyOf(t.getStackTrace(), m + 1, StackTraceElement[].class));
    t.printStackTrace(new PrintWriter(writer));
    t.setStackTrace(tStack);
    StringBuilder sb = new StringBuilder(writer.toString());
    if (framesInCommon != 0) {
        sb.append("\t... " + framesInCommon + " more");
    }
    return sb.toString();
}

From source file:org.jdbcluster.clustertype.ClusterTypeFactory.java

/**
 * creates ClusterType objects depending on the ClusterType name which is passed as
 * an argument//from w ww . j  av a2s . co m
 * @param <T> is a template which extends the interface ClusterType
 * @param clusterTypeName the ClusterType's name as a String
 * @return clusterType the ClusterType object
 * @throws ClusterTypeException
 */
@SuppressWarnings("unchecked")
static public <T extends ClusterType> T newInstance(String clusterTypeName) throws ClusterTypeException {

    Assert.notNull(clusterTypeName, "clusterTypeName  may not be null");
    Assert.hasLength(clusterTypeName, "clusterTypeName  may not have zero length");

    //get the classname of the object
    String className = ClusterTypeBase.getClusterTypeConfig().getClusterClassName(clusterTypeName);
    //if no name was defined, throw an exception
    if (className == null) {
        throw new ClusterTypeException("No ClusterType of type " + clusterTypeName + " found!",
                new Throwable());
    }

    ClusterTypeBase clusterType = new ClusterTypeImpl();

    //save the name
    clusterType.setName(clusterTypeName);
    return (T) clusterType;
}

From source file:com.example.ryan.weixindemo.util.DebugLog.java

public static void d(String message) {
    if (!isDebuggable())
        return;/*w  w w . ja va  2  s.  c o  m*/

    getMethodNames(new Throwable().getStackTrace());
    Log.d(className, createLog(message));
}

From source file:org.apache.drill.exec.store.mapr.db.json.OjaiFunctionsProcessor.java

private static String getStackTrace() {
    final Throwable throwable = new Throwable();
    final StackTraceElement[] ste = throwable.getStackTrace();
    final StringBuilder sb = new StringBuilder();
    for (int i = 1; i < ste.length; ++i) {
        sb.append(ste[i].toString());// w  w  w  .j a  va  2s.  c  o m
        sb.append('\n');
    }

    return sb.toString();
}

From source file:com.example.ryan.weixindemo.util.DebugLog.java

public static void v(String message) {
    if (!isDebuggable())
        return;//  w  w  w .  j  a  va 2 s  .  c  o  m

    getMethodNames(new Throwable().getStackTrace());
    Log.v(className, createLog(message));
}

From source file:com.rsmart.rfabric.logging.FormattedLogger.java

/**
 * Uses {@link StackTraceElement[]} from {@link Throwable} to determine the calling class. Then, the {@link Log} is retrieved for it by
 * convention//from   w ww. java 2s .c o m
 * 
 * 
 * @return Log for the calling class
 */
private static final Log getLog() {
    try {
        return LogFactory.getLog(Class.forName(new Throwable().getStackTrace()[3].getClassName()));
    } catch (Exception e) {
        return LogFactory.getLog(FormattedLogger.class);
    }
}

From source file:com.willwinder.universalgcodesender.utils.GUIHelpers.java

/**
 * Displays an error message to the user.
 * @param errorMessage message to display in the dialog.
 * @param modal toggle whether the message should block or fire and forget.
 *///from  w w  w  . jav  a2s .  c  o  m
public static void displayErrorDialog(final String errorMessage, boolean modal) {
    if (StringUtils.isEmpty(errorMessage)) {
        LOGGER.warning("Something tried to display an error message with an empty message: "
                + ExceptionUtils.getStackTrace(new Throwable()));
        return;
    }

    Runnable r = () -> {
        //JOptionPane.showMessageDialog(new JFrame(), errorMessage, 
        //        Localization.getString("error"), JOptionPane.ERROR_MESSAGE);
        NarrowOptionPane.showNarrowDialog(250, errorMessage.replaceAll("\\.\\.", "\\."),
                Localization.getString("error"), JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
    };

    if (modal) {
        r.run();
    } else {
        java.awt.EventQueue.invokeLater(r);
    }
}

From source file:com.facebook.infrastructure.utils.FBUtilities.java

public static String getCurrentThreadStackTrace() {
    Throwable throwable = new Throwable();
    StackTraceElement[] ste = throwable.getStackTrace();
    StringBuffer sbuf = new StringBuffer();

    for (int i = ste.length - 1; i > 0; --i) {
        sbuf.append(ste[i].getClassName() + "." + ste[i].getMethodName());
        sbuf.append("/");
    }//from  w ww . ja va2 s  .  c  o m
    sbuf.deleteCharAt(sbuf.length() - 1);
    return sbuf.toString();
}

From source file:com.example.ryan.weixindemo.util.DebugLog.java

public static void w(String message) {
    if (!isDebuggable())
        return;/*from ww  w .ja  v a 2s .  c o m*/

    getMethodNames(new Throwable().getStackTrace());
    Log.w(className, createLog(message));
}

From source file:de.tudarmstadt.ukp.wikipedia.util.DbUtilities.java

public boolean tableExists(String tableName) {

    try {//  w  w w . j  a va  2 s. c om
        DatabaseMetaData dbmd = conn.getMetaData();

        // Specify the type of object; in this case we want tables
        String[] types = { "TABLE" };

        // get all table names
        ResultSet resultSet = dbmd.getTables(null, null, "%", types);

        while (resultSet.next()) {
            if (resultSet.getString("TABLE_NAME").equals(tableName)) {
                return true;
            }
        }
    } catch (SQLException e) {
        logger.error("Table " + tableName + " does not exist.", new Throwable());
    }

    return false;
}