Example usage for java.lang Throwable getStackTrace

List of usage examples for java.lang Throwable getStackTrace

Introduction

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

Prototype

public StackTraceElement[] getStackTrace() 

Source Link

Document

Provides programmatic access to the stack trace information printed by #printStackTrace() .

Usage

From source file:com.tcl.lzhang1.mymusic.AppException.java

/**
 * Gets the crash report./*from w w w .  j  ava  2  s .com*/
 * 
 * @param context the context
 * @param ex the ex
 * @return the crash report
 */
private String getCrashReport(Context context, Throwable ex) {
    PackageInfo pinfo = ((AppContext) context.getApplicationContext()).getPackageInfo();
    StringBuffer exceptionStr = new StringBuffer();
    exceptionStr.append("Version: " + pinfo.versionName + "(" + pinfo.versionCode + ")\n");
    exceptionStr.append("Android: " + android.os.Build.VERSION.RELEASE + "(" + android.os.Build.MODEL + ")\n");
    exceptionStr.append("Exception: " + ex.getMessage() + "\n");
    StackTraceElement[] elements = ex.getStackTrace();
    for (int i = 0; i < elements.length; i++) {
        exceptionStr.append(elements[i].toString() + "\n");
    }
    return exceptionStr.toString();

}

From source file:com.gargoylesoftware.htmlunit.WebTestCase.java

/**
 * Finds from the call stack the active running JUnit test case
 * @return the test case method//from  w w w.j a v a 2s . c o m
 * @throws RuntimeException if no method could be found
 */
private Method findRunningJUnitTestMethod() {
    final Class<?> cl = getClass();
    final Class<?>[] args = new Class[] {};

    // search the initial junit test
    final Throwable t = new Exception();
    for (int i = t.getStackTrace().length - 1; i >= 0; i--) {
        final StackTraceElement element = t.getStackTrace()[i];
        if (element.getClassName().equals(cl.getName())) {
            try {
                final Method m = cl.getMethod(element.getMethodName(), args);
                if (isPublicTestMethod(m)) {
                    return m;
                }
            } catch (final Exception e) {
                // can't access, ignore it
            }
        }
    }
    throw new RuntimeException("No JUnit test case method found in call stack");
}

From source file:mondrian.test.DiffRepository.java

/**
 * Returns the name of the current testcase by looking up the call
 * stack for a method whose name starts with "test", for example
 * "testFoo"./* ww w  .j a  v a2s  .  c o  m*/
 *
 * @param fail Whether to fail if no method is found
 * @return Name of current testcase, or null if not found
 */
public String getCurrentTestCaseName(boolean fail) {
    // check thread-local first
    String testCaseName = CurrentTestCaseName.get();
    if (testCaseName != null) {
        return testCaseName;
    }

    // Clever, this. Dump the stack and look up it for a method which
    // looks like a testcase name, e.g. "testFoo".
    final StackTraceElement[] stackTrace;
    //noinspection ThrowableInstanceNeverThrown
    Throwable runtimeException = new Throwable();
    runtimeException.fillInStackTrace();
    stackTrace = runtimeException.getStackTrace();
    for (StackTraceElement stackTraceElement : stackTrace) {
        final String methodName = stackTraceElement.getMethodName();
        if (methodName.startsWith("test")) {
            return methodName;
        }
    }
    if (fail) {
        throw new RuntimeException("no testcase on current callstack");
    } else {
        return null;
    }
}

From source file:eu.faircode.adblocker.Util.java

public static void sendCrashReport(Throwable ex, final Context context) {
    if (!isPlayStoreInstall(context))
        return;/*  www  .j  a  v a  2  s  .c  o  m*/

    try {
        ApplicationErrorReport report = new ApplicationErrorReport();
        report.packageName = report.processName = context.getPackageName();
        report.time = System.currentTimeMillis();
        report.type = ApplicationErrorReport.TYPE_CRASH;
        report.systemApp = false;

        ApplicationErrorReport.CrashInfo crash = new ApplicationErrorReport.CrashInfo();
        crash.exceptionClassName = ex.getClass().getSimpleName();
        crash.exceptionMessage = ex.getMessage();

        StringWriter writer = new StringWriter();
        PrintWriter printer = new PrintWriter(writer);
        ex.printStackTrace(printer);

        crash.stackTrace = writer.toString();

        StackTraceElement stack = ex.getStackTrace()[0];
        crash.throwClassName = stack.getClassName();
        crash.throwFileName = stack.getFileName();
        crash.throwLineNumber = stack.getLineNumber();
        crash.throwMethodName = stack.getMethodName();

        report.crashInfo = crash;

        final Intent bug = new Intent(Intent.ACTION_APP_ERROR);
        bug.putExtra(Intent.EXTRA_BUG_REPORT, report);
        bug.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (bug.resolveActivity(context.getPackageManager()) != null)
            context.startActivity(bug);
    } catch (Throwable exex) {
        Log.e(TAG, exex.toString() + "\n" + Log.getStackTraceString(exex));
    }
}

From source file:org.apache.hadoop.hbase.coprocessor.TestAggregateProtocol.java

/**
 * This will test the row count with startrow > endrow. The result should be
 * -1.// w  ww  .  ja  v  a 2s  .c  o m
 * @throws Throwable
 */
@Test(timeout = 300000)
public void testRowCountWithInvalidRange1() {
    AggregationClient aClient = new AggregationClient(conf);
    Scan scan = new Scan();
    scan.setStartRow(ROWS[5]);
    scan.setStopRow(ROWS[2]);

    final ColumnInterpreter<Long, Long, EmptyMsg, LongMsg, LongMsg> ci = new LongColumnInterpreter();
    long rowCount = -1;
    try {
        rowCount = aClient.rowCount(TEST_TABLE, ci, scan);
    } catch (Throwable e) {
        myLog.error("Exception thrown in the invalidRange method" + e.getStackTrace());
    }
    assertEquals(-1, rowCount);
}

From source file:org.alfresco.repo.web.scripts.bulkimport.AbstractBulkFileSystemImportWebScript.java

private String renderExceptionStackAsText(Throwable t) {
    StringBuffer result = new StringBuffer();

    if (t != null) {
        String message = t.getMessage();
        Throwable cause = t.getCause();

        if (cause != null) {
            result.append(renderExceptionStackAsText(cause));
            result.append("\nWrapped by:");
        }/*from ww w.j  a  v a2s .c om*/

        if (message == null) {
            message = "";
        }

        result.append("\n");
        result.append(t.getClass().getName());
        result.append(": ");
        result.append(message);
        result.append("\n");
        result.append(renderStackTraceElements(t.getStackTrace()));
    }

    return (result.toString());
}

From source file:com.galeoconsulting.leonardinius.rest.service.ScriptRunner.java

private String getStackTrace(Throwable th) {
    if (th == null) {
        return "";
    }/*  w w  w  . ja  v  a  2 s.  c om*/

    List<StackTraceElement> elements = Lists.newArrayList();
    for (StackTraceElement st : th.getStackTrace()) {
        if (st.getClassName().equals(CLASS_NAME))
            break;
        elements.add(st);
    }

    return new StringBuilder(ExceptionUtils.getMessage(th)).append(" at ")
            .append(Joiner.on("\n ").skipNulls().join(elements)).toString();
}

From source file:android_network.hetnet.vpn_service.Util.java

public static void sendCrashReport(Throwable ex, final Context context) {
    if (!isPlayStoreInstall(context) || Util.isDebuggable(context))
        return;// w  w w.j a  va  2  s .  c om

    try {
        ApplicationErrorReport report = new ApplicationErrorReport();
        report.packageName = report.processName = context.getPackageName();
        report.time = System.currentTimeMillis();
        report.type = ApplicationErrorReport.TYPE_CRASH;
        report.systemApp = false;

        ApplicationErrorReport.CrashInfo crash = new ApplicationErrorReport.CrashInfo();
        crash.exceptionClassName = ex.getClass().getSimpleName();
        crash.exceptionMessage = ex.getMessage();

        StringWriter writer = new StringWriter();
        PrintWriter printer = new PrintWriter(writer);
        ex.printStackTrace(printer);

        crash.stackTrace = writer.toString();

        StackTraceElement stack = ex.getStackTrace()[0];
        crash.throwClassName = stack.getClassName();
        crash.throwFileName = stack.getFileName();
        crash.throwLineNumber = stack.getLineNumber();
        crash.throwMethodName = stack.getMethodName();

        report.crashInfo = crash;

        final Intent bug = new Intent(Intent.ACTION_APP_ERROR);
        bug.putExtra(Intent.EXTRA_BUG_REPORT, report);
        bug.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (bug.resolveActivity(context.getPackageManager()) != null)
            context.startActivity(bug);
    } catch (Throwable exex) {
        Log.e(TAG, exex.toString() + "\n" + Log.getStackTraceString(exex));
    }
}

From source file:de.axelfaust.alfresco.nashorn.repo.web.scripts.console.ExecutePost.java

protected void collectCallstackLines(final Throwable exception, final List<String> callstackLines) {
    if (exception.getCause() != null) {
        this.collectCallstackLines(exception.getCause(), callstackLines);

        callstackLines.add("Wrapped in " + exception.toString());
        callstackLines.add(CALLSTACK_AT_PREFIX + exception.getStackTrace()[0].toString());
    } else {//w w  w  .  j a va  2s. co  m
        callstackLines.add(exception.toString());
        for (final StackTraceElement element : exception.getStackTrace()) {
            callstackLines.add(CALLSTACK_AT_PREFIX + element.toString());
        }
    }
}

From source file:org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.Launcher.java

/**
 * // w  ww  . j  a va 2s.com
 * @param stackTraceLine The string representation of {@link Throwable#printStackTrace() printStackTrace}
 * Handles internal PigException and its subclasses that override the {@link Throwable#toString() toString} method
 * @return An exception object whose string representation of printStackTrace is the input stackTrace 
 * @throws Exception
 */
Exception getExceptionFromString(String stackTrace) throws Exception {
    String[] lines = stackTrace.split(newLine);
    Throwable t = getExceptionFromStrings(lines, 0);

    if (!pigException) {
        int errCode = 6015;
        String msg = "During execution, encountered a Hadoop error.";
        ExecException ee = new ExecException(msg, errCode, PigException.REMOTE_ENVIRONMENT, t);
        ee.setStackTrace(t.getStackTrace());
        return ee;
    } else {
        pigException = false;
        if (outOfMemory) {
            outOfMemory = false;
            int errCode = 6016;
            String msg = "Out of memory.";
            ExecException ee = new ExecException(msg, errCode, PigException.REMOTE_ENVIRONMENT, t);
            ee.setStackTrace(t.getStackTrace());
            return ee;
        }
        return (Exception) t;
    }
}