Here you can find the source of getStackTrace(Throwable exc)
Parameter | Description |
---|---|
exc | the exception |
public static String[] getStackTrace(Throwable exc)
//package com.java2s; /******************************************************************************* * Copyright (c) 2001, 2005 IBM Corporation 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:// w w w. ja v a 2 s .c o m * IBM Corporation - initial API and implementation *******************************************************************************/ import java.io.BufferedReader; import java.io.PrintWriter; import java.io.StringReader; import java.io.StringWriter; import java.util.Vector; public class Main { /** * Creates and array of strings containing the exception traceback information of * a Throwable. This is the same traceback data that is displayed by exc.printStackTrace(). * @param exc the exception * @return a string array of the traceback information. */ public static String[] getStackTrace(Throwable exc) { Vector lines = new Vector(); StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); exc.printStackTrace(printWriter); try { printWriter.close(); stringWriter.close(); } catch (Exception nestedExc) { return new String[0]; } StringReader stringReader = new StringReader(stringWriter.toString()); BufferedReader reader = new BufferedReader(stringReader); String line = null; try { line = reader.readLine(); while (line != null) { lines.add(line.trim()); line = reader.readLine(); } } catch (Exception nestedExc) { return new String[0]; } return (String[]) lines.toArray(new String[0]); } }