Example usage for java.lang Exception setStackTrace

List of usage examples for java.lang Exception setStackTrace

Introduction

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

Prototype

public void setStackTrace(StackTraceElement[] stackTrace) 

Source Link

Document

Sets the stack trace elements that will be returned by #getStackTrace() and printed by #printStackTrace() and related methods.

Usage

From source file:org.commonjava.indy.ftest.core.fixture.ThreadDumper.java

public static TestRule timeoutRule(int timeout, TimeUnit units) {
    return (base, description) -> new Statement() {
        public void evaluate() throws Throwable {
            System.out.printf("Setting up timeout: %d %s to wrap: %s\n", timeout, units, base);
            AtomicReference<Throwable> error = new AtomicReference<>();
            CountDownLatch latch = new CountDownLatch(1);
            FutureTask<Void> task = new FutureTask<>(() -> {
                try {
                    latch.countDown();/*from ww  w . ja v  a 2s  .co  m*/
                    base.evaluate();
                } catch (Throwable t) {
                    error.set(t);
                }

                return null;
            });

            ThreadGroup tg = new ThreadGroup("Test Timeout Group");
            Thread t = new Thread(tg, task, "Test Timeout Thread");
            t.setDaemon(true);
            t.start();

            try {
                System.out.println("Waiting for test to start.");
                latch.await();
            } catch (InterruptedException e) {
                error.set(e);
            }

            if (error.get() == null) {
                try {
                    System.out.println("Waiting for test to complete (or timeout)");
                    task.get(timeout, units);
                } catch (InterruptedException e) {
                    error.set(e);
                } catch (ExecutionException e) {
                    error.set(e.getCause());
                } catch (TimeoutException e) {
                    System.out.printf("Test timeout %d %s expired!\n", timeout, units.name());
                    dumpThreads();
                    StackTraceElement[] stackTrace = t.getStackTrace();
                    Exception currThreadException = new TestTimedOutException(timeout, units);
                    if (stackTrace != null) {
                        currThreadException.setStackTrace(stackTrace);
                        t.interrupt();
                    }

                    throw currThreadException;
                }
            }

            Throwable throwable = error.get();
            if (throwable != null) {
                throw throwable;
            }
        }
    };
}

From source file:org.LexGrid.LexBIG.caCore.client.proxy.LexEVSApplicationServiceProxy.java

public static Exception digOutRealExceptionAndThrowIt(Exception e) throws Exception {
    Throwable lbiEx = null;/*  ww  w.  ja v a 2  s.  com*/
    Throwable next = null;
    Throwable cur = e;
    boolean lexBigExceptionFound = false;
    boolean done = false;
    int i = 1;
    int max = 500; // a limit so we won't ever be in endless loop
    if (isLexBigException(cur) == true) {
        lexBigExceptionFound = true;
        lbiEx = cur;
    }
    while (!done) {
        next = cur.getCause();
        ++i;
        if (next == null) {
            done = true;
        } else {
            cur = next;
            if (isLexBigException(cur) == true) {
                lexBigExceptionFound = true;
                lbiEx = cur;
            }
            if (i == max) {
                done = true;
                log.error("digOutRealExceptionAndThrowIt: reached max depth of: " + max);
            }
        }
    }
    Exception returnException = null;
    if (lexBigExceptionFound == true) {
        returnException = new LBException(lbiEx.getMessage());
        returnException.setStackTrace(lbiEx.getStackTrace());
    } else {
        returnException = e;
    }
    return returnException;
}

From source file:org.eclipse.recommenders.internal.stacktraces.rcp.LogListenerTest.java

private static Status createErrorStatus() {
    Exception e1 = new RuntimeException();
    StackTraceElement[] trace = ErrorReportsDTOs.createStacktraceForClasses("A", "D", "C");
    e1.setStackTrace(trace);
    return new Status(IStatus.ERROR, TEST_PLUGIN_ID, "test message", e1);
}

From source file:crittercism.CrittercismModuleModule.java

private static Exception createException(String name, String reason, String stackTrace) {
    Exception ex = new Exception(reason);
    if (stackTrace != null) {
        String[] stackObjs = stackTrace.split("\n");

        int nLength = stackObjs.length;
        StackTraceElement[] elements = new StackTraceElement[nLength];

        for (int nI = 0; nI < nLength; nI++) {
            elements[nI] = new StackTraceElement("Unity3D", stackObjs[nI], "", -1);
        }//from   ww  w  .jav  a 2 s  .  c om
        ex.setStackTrace(elements);
    }
    ;

    return ex;
}

From source file:org.ow2.proactive_grid_cloud_portal.scheduler.client.SchedulerRestClient.java

private static Exception rebuildServerSideException(ExceptionToJson json) throws IllegalArgumentException,
        InstantiationException, IllegalAccessException, InvocationTargetException {
    Throwable serverException = json.getException();
    String exceptionClassName = json.getExceptionClass();
    String errMsg = json.getErrorMessage();
    if (errMsg == null) {
        errMsg = "An error has occurred.";
    }//from   ww w  .ja v  a  2 s . c  o m

    if (serverException != null && exceptionClassName != null) {
        Class<?> exceptionClass = toClass(exceptionClassName);
        if (exceptionClass != null) {
            // wrap the exception serialized in JSON inside an
            // instance of
            // the server exception class
            Constructor<?> constructor = getConstructor(exceptionClass, Throwable.class);
            if (constructor != null) {
                return (Exception) constructor.newInstance(serverException);
            }
            constructor = getConstructor(exceptionClass, String.class);
            if (constructor != null) {
                Exception built = (Exception) constructor.newInstance(errMsg);
                built.setStackTrace(serverException.getStackTrace());
                return built;
            }
        }
    }

    Exception built = new Exception(errMsg);
    if (serverException != null) {
        built.setStackTrace(serverException.getStackTrace());
    }
    return built;
}

From source file:br.com.nordestefomento.jrimum.utilix.Field.java

private static Exception getGenericReadError(Exception e, String value) {

    StackTraceElement[] stackTrace = e.getStackTrace();
    e = new RuntimeException("VALOR INV?LIDO [ " + value + " ]!\nCausado por: " + e.getCause());
    e.setStackTrace(stackTrace);

    return e;//w  ww .  j  a v a 2s.c o  m
}

From source file:org.jrimum.utilix.text.Field.java

/**
 * Formata a exceo caso ela ocorra na leitura do campo.
 * @param e//from w ww .ja  v a2  s  .c o m
 * @param value
 * @return
 */
private static Exception getGenericReadError(Exception e, String value) {

    StackTraceElement[] stackTrace = e.getStackTrace();
    e = new RuntimeException("VALOR INV?LIDO [ " + value + " ]!\nCausado por: " + e.getCause());
    e.setStackTrace(stackTrace);

    return e;
}

From source file:ORG.oclc.os.SRW.SRWDiagnostic.java

public static de.escidoc.core.domain.sru.diagnostics.DiagnosticType newSruDiagnosticType(String baseURI,
        int code, String details) {
    de.escidoc.core.domain.sru.diagnostics.DiagnosticType dt = new de.escidoc.core.domain.sru.diagnostics.DiagnosticType();
    dt.setUri(baseURI + code);/*  w  ww.j  av  a2 s .co  m*/
    dt.setDetails(details);
    if (log.isDebugEnabled()) {
        try {
            log.debug(SRWDiagnostic.message[code] + "(" + code + "): \"" + details + "\"");
        } catch (Exception e) {
            log.error("Diagnostic code " + code + " not in SRWDiagnostics");
            log.error(code + ": \"" + details + "\"");
        }
        if (code != 10 && code != 16 && code != 22 && code != 130) {
            Exception e = new Exception();
            StackTraceElement[] trace = e.getStackTrace();
            int i;
            String className;
            for (i = 0; i < trace.length; i++) {
                className = trace[i].getClassName();
                if (!className.startsWith("ORG") && !className.startsWith("gov"))
                    break;
            }
            StackTraceElement[] shortTrace = new StackTraceElement[i];
            System.arraycopy(trace, 0, shortTrace, 0, i);
            e.setStackTrace(shortTrace);
            log.debug(e, e);
        }
    }
    return dt;
}

From source file:org.eclipse.recommenders.stacktraces.rcp.actions.LogErrorsAction.java

private void logMultiStatusDelayed() {
    int counter = 0;
    IStatus[] children = new IStatus[3];
    for (int i = 0; i < children.length; i++) {
        RuntimeException cause = new IllegalArgumentException("cause" + i);
        cause.setStackTrace(createTrace(3));
        Exception exception = new RuntimeException("exception message", cause);
        exception.setStackTrace(createTrace(3));
        children[i] = new Status(IStatus.ERROR, "org.eclipse.recommenders.stacktraces.rcp",
                "status error message " + ++counter, exception);
    }/*from www.  j a v  a  2  s  . c om*/
    try {
        Thread.sleep(750);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    log.log(new MultiStatus("org.eclipse.recommenders.stacktraces.rcp", IStatus.ERROR, children,
            "status error message", new RuntimeException()));
}

From source file:com.jkoolcloud.tnt4j.utils.Utils.java

/**
 * Print given message, stack trace to the underlying print stream
 *
 * @param msg/*from  w  w  w .ja v  a 2  s .c o m*/
 *            user defined message
 * @param trace
 *            stack trace
 * @param out
 *            print stream where output is written
 */
public static void printStackTrace(String msg, StackTraceElement[] trace, PrintStream out) {
    Exception ex = new Exception(msg);
    ex.setStackTrace(trace);
    ex.printStackTrace(out);
}