java.lang.Exception.getStackTrace()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.1k)|赞(0)|评价(0)|浏览(695)

本文整理了Java中java.lang.Exception.getStackTrace()方法的一些代码示例,展示了Exception.getStackTrace()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Exception.getStackTrace()方法的具体详情如下:
包路径:java.lang.Exception
类名称:Exception
方法名:getStackTrace

Exception.getStackTrace介绍

暂无

代码示例

代码示例来源:origin: jenkinsci/jenkins

public LegacyCodeCause() {
  stackTrace = new Exception().getStackTrace();
}

代码示例来源:origin: redisson/redisson

/**
 * Returns current stack trace in form of array of stack trace elements.
 * First stack trace element is removed.
 * Since an exception is thrown internally, this method is slow.
 */
@SuppressWarnings({"ThrowCaughtLocally"})
public static StackTraceElement[] getCurrentStackTrace() {
  StackTraceElement[] ste = new Exception().getStackTrace();
  if (ste.length > 1) {
    StackTraceElement[] result = new StackTraceElement[ste.length - 1];
    System.arraycopy(ste, 1, result, 0, ste.length - 1);
    return result;
  } else {
    return ste;
  }
}

代码示例来源:origin: google/guava

private static Exception throwCause(Exception e, boolean combineStackTraces) throws Exception {
 Throwable cause = e.getCause();
 if (cause == null) {
  throw e;
 }
 if (combineStackTraces) {
  StackTraceElement[] combined =
    ObjectArrays.concat(cause.getStackTrace(), e.getStackTrace(), StackTraceElement.class);
  cause.setStackTrace(combined);
 }
 if (cause instanceof Exception) {
  throw (Exception) cause;
 }
 if (cause instanceof Error) {
  throw (Error) cause;
 }
 // The cause is a weird kind of Throwable, so throw the outer exception.
 throw e;
}

代码示例来源:origin: aa112901/remusic

public static String getExceptionMessage(Exception ex) {
  String result = "";
  StackTraceElement[] stes = ex.getStackTrace();
  for (int i = 0; i < stes.length; i++) {
    result = result + stes[i].getClassName() + "." + stes[i].getMethodName() + "  " + stes[i].getLineNumber()
        + "line" + "\r\n";
  }
  return result;
}

代码示例来源:origin: h2oai/h2o-2

public HTTP500V1( Exception e ) { 
 error = e.getClass().getSimpleName()+": "+e.getMessage();
 stackTrace = Arrays.toString(e.getStackTrace());
}
@Override public HTTP500V1 fillInto( Handler h ) { throw H2O.fail(); }

代码示例来源:origin: airbnb/epoxy

EpoxyProcessorException(Exception e, String message) {
 super(message, e);
 setStackTrace(e.getStackTrace());
}

代码示例来源:origin: gocd/gocd

public static void addDeveloperErrorMessage(Map jsonMap, Exception e) {
  addFriendlyErrorMessage(jsonMap, e.getMessage() + ": " + e.getCause() + " at " + e.getStackTrace()[0]);
}

代码示例来源:origin: prestodb/presto

private static Exception throwCause(Exception e, boolean combineStackTraces) throws Exception {
 Throwable cause = e.getCause();
 if (cause == null) {
  throw e;
 }
 if (combineStackTraces) {
  StackTraceElement[] combined =
    ObjectArrays.concat(cause.getStackTrace(), e.getStackTrace(), StackTraceElement.class);
  cause.setStackTrace(combined);
 }
 if (cause instanceof Exception) {
  throw (Exception) cause;
 }
 if (cause instanceof Error) {
  throw (Error) cause;
 }
 // The cause is a weird kind of Throwable, so throw the outer exception.
 throw e;
}

代码示例来源:origin: quartz-scheduler/quartz

private Exception newPlainException(Exception e) {
  String type = e.getClass().getName();
  if(type.startsWith("java.") || type.startsWith("javax.")) {
    return e;
  } else {
    Exception result = new Exception(e.getMessage());
    result.setStackTrace(e.getStackTrace());
    return result;
  }
}

代码示例来源:origin: wildfly/wildfly

private static <T extends Throwable> T copyContents(final Exception source, final T throwable) {
    throwable.setStackTrace(source.getStackTrace());
    final Throwable[] suppressed = source.getSuppressed();
    if (suppressed != null) for (final Throwable t : suppressed) {
      throwable.addSuppressed(t);
    }
    return throwable;
  }
}

代码示例来源:origin: h2oai/h2o-2

public static void POST(int n, Exception e) {
  if (e.getMessage() != null) {
    POST(n, e.getMessage());
  }
  POST(n, e.toString());
  StackTraceElement[] els = e.getStackTrace();
  for (int i = 0; i < els.length; i++) {
    POST(n, els[i].toString());
  }
}
public static void POST(int n) {

代码示例来源:origin: spring-projects/spring-framework

@Test
public void fillInClientStackTraceIfPossibleSunnyDay() throws Exception {
  try {
    throw new IllegalStateException("Mmm");
  }
  catch (Exception ex) {
    int originalStackTraceLngth = ex.getStackTrace().length;
    RemoteInvocationUtils.fillInClientStackTraceIfPossible(ex);
    assertTrue("Stack trace not being filled in",
        ex.getStackTrace().length > originalStackTraceLngth);
  }
}

代码示例来源:origin: spring-projects/spring-framework

StackTraceElement[] callStack = new Exception().getStackTrace();
StackTraceElement[] cachedCallStack = exception.getStackTrace();

代码示例来源:origin: eclipse-vertx/vert.x

protected void afterAsyncTestBase() {
 if (throwable != null && thrownThread != Thread.currentThread() && !awaitCalled) {
  // Throwable caught from non main thread
  throw new IllegalStateException("Assert or failure from non main thread but no await() on main thread", throwable);
 }
 if (lateFailure) {
  throw new IllegalStateException("Test reported a failure after completion");
 }
 for (Map.Entry<String, Exception> entry: threadNames.entrySet()) {
  if (!entry.getKey().equals(mainThreadName)) {
   if (threadChecksEnabled && !entry.getKey().startsWith("vert.x-")) {
    IllegalStateException is = new IllegalStateException("Non Vert.x thread! :" + entry.getKey());
    is.setStackTrace(entry.getValue().getStackTrace());
    throw is;
   }
  }
 }
}

代码示例来源:origin: SonarSource/sonarqube

public static RuntimeException translateException(String msg, Exception cause) {
 RuntimeException runtimeException = new RuntimeException(String.format("%s: [%s] %s", msg, cause.getClass().getName(), cause.getMessage()));
 runtimeException.setStackTrace(cause.getStackTrace());
 return runtimeException;
}

代码示例来源:origin: apache/hive

static void debugTrace() {
 if (LOG.isDebugEnabled()){
  Exception e = new Exception();
  LOG.debug("." + e.getStackTrace()[1].getLineNumber() + ":" + TestObjectStoreInitRetry.getInjectConnectFailure());
 }
}

代码示例来源:origin: gocd/gocd

void cleanAndResetToMaster() throws IOException {
  try {
    git.reset().setMode(ResetCommand.ResetType.HARD).call();
    checkout("master");
    deleteBranch(BRANCH_AT_REVISION);
    deleteBranch(BRANCH_AT_HEAD);
  } catch (Exception e) {
    String currentBranch = git.getRepository().getBranch();
    LOGGER.error("Error while trying to clean up config repository, CurrentBranch: {} \n : \n Message: {} \n StackTrace: {}", currentBranch, e.getMessage(), e.getStackTrace(), e);
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void findMethodAnnotationOnBridgeMethod() throws Exception {
  Method bridgeMethod = SimpleFoo.class.getMethod("something", Object.class);
  assertTrue(bridgeMethod.isBridge());
  assertNull(bridgeMethod.getAnnotation(Order.class));
  assertNull(getAnnotation(bridgeMethod, Order.class));
  assertNotNull(findAnnotation(bridgeMethod, Order.class));
  boolean runningInEclipse = Arrays.stream(new Exception().getStackTrace())
      .anyMatch(element -> element.getClassName().startsWith("org.eclipse.jdt"));
  // As of JDK 8, invoking getAnnotation() on a bridge method actually finds an
  // annotation on its 'bridged' method [1]; however, the Eclipse compiler will not
  // support this until Eclipse 4.9 [2]. Thus, we effectively ignore the following
  // assertion if the test is currently executing within the Eclipse IDE.
  //
  // [1] https://bugs.openjdk.java.net/browse/JDK-6695379
  // [2] https://bugs.eclipse.org/bugs/show_bug.cgi?id=495396
  //
  if (!runningInEclipse) {
    assertNotNull(bridgeMethod.getAnnotation(Transactional.class));
  }
  assertNotNull(getAnnotation(bridgeMethod, Transactional.class));
  assertNotNull(findAnnotation(bridgeMethod, Transactional.class));
}

代码示例来源:origin: google/guava

@GwtIncompatible // lazyStackTrace
private void doTestLazyStackTraceFallback() {
 assertFalse(lazyStackTraceIsLazy());
 Exception e = new Exception();
 assertThat(lazyStackTrace(e)).containsExactly((Object[]) e.getStackTrace()).inOrder();
 try {
  lazyStackTrace(e).set(0, null);
  fail();
 } catch (UnsupportedOperationException expected) {
 }
 e.setStackTrace(new StackTraceElement[0]);
 assertThat(lazyStackTrace(e)).isEmpty();
}

代码示例来源:origin: google/guava

@GwtIncompatible // lazyStackTrace(Throwable)
public void testLazyStackTrace() {
 Exception e = new Exception();
 StackTraceElement[] originalStackTrace = e.getStackTrace();
 assertThat(lazyStackTrace(e)).containsExactly((Object[]) originalStackTrace).inOrder();
 try {
  lazyStackTrace(e).set(0, null);
  fail();
 } catch (UnsupportedOperationException expected) {
 }
 // Now we test a property that holds only for the lazy implementation.
 if (!lazyStackTraceIsLazy()) {
  return;
 }
 e.setStackTrace(new StackTraceElement[0]);
 assertThat(lazyStackTrace(e)).containsExactly((Object[]) originalStackTrace).inOrder();
}

相关文章