Java Stacktrace stackTrace(int depth)

Here you can find the source of stackTrace(int depth)

Description

stack Trace

License

Open Source License

Declaration

public static void stackTrace(int depth) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from  w w w.  j  a v a 2 s  .com
 *     Randy Rohrbach (Wind River Systems, Inc.) - Copied and modified to create the floating point plugin
 *******************************************************************************/

public class Main {
    public static void stackTrace(int depth) {
        int offset = 3; // Ignore frames contributed to the stack based on call to this method
        if (depth == 0)
            depth = 4; // Default depth if zero supplied

        // Get the stack frames for the current thread; start at the offset  

        StackTraceElement[] seArray = Thread.currentThread()
                .getStackTrace();

        if (seArray.length > offset) {
            System.out
                    .println("Displaying " + depth + " of " + seArray.length + " stack trace elements"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            for (int index = offset; index < Math.min(depth + offset,
                    seArray.length + offset); index++)
                System.out
                        .println("   " + seArray[index].getClassName() + "." + seArray[index].getMethodName() + ": line " + seArray[index].getLineNumber()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        } else
            System.out.println("No stack frames to display"); //$NON-NLS-1$
    }
}

Related

  1. stackTrace()
  2. stackTrace()
  3. stacktrace()
  4. stackTrace(final Exception t)
  5. stackTrace(final Throwable caught)
  6. stackTrace(StackTraceElement[] stackTrace)
  7. stackTrace2String(Throwable e)
  8. stackTraceApproved(StackTraceElement elt, String[] approved)
  9. stackTraceAsString(Throwable t)