Android Throwable Method Get getThrowingMethod(Throwable t)

Here you can find the source of getThrowingMethod(Throwable t)

Description

Returns the class and method name (+a line number) in which the Throwable was thrown.

License

Open Source License

Parameter

Parameter Description
t A Throwable to analyze.

Return

A human-readable string stating the class and method. Do not rely the format to be anything fixed.

Declaration

public static String getThrowingMethod(Throwable t) 

Method Source Code

/*//from  www . j  av  a  2 s. c  o m
  Copyright (c) Inexas 2010

  Modifications licensed under the Inexas Software License V1.0. You
  may not use this file except in compliance with the License.

  The License is available at: http://www.inexas.com/ISL-V1.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  The original file and contents are licensed under a separate license:
  see below.
 */

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import org.apache.log4j.Logger;

public class Main{
    /**
     * Returns the class and method name (+a line number) in which the Throwable
     * was thrown.
     * 
     * @param t
     *            A Throwable to analyze.
     * @return A human-readable string stating the class and method. Do not rely
     *         the format to be anything fixed.
     */
    public static String getThrowingMethod(Throwable t) {
        StackTraceElement[] trace = t.getStackTrace();
        StringBuffer sb = new StringBuffer();

        if (trace == null || trace.length == 0) {
            sb.append("[Stack trace not available]");
        } else {
            sb.append(trace[0].isNativeMethod() ? "native method" : "");
            sb.append(trace[0].getClassName());
            sb.append(".");
            sb.append(trace[0].getMethodName() + "(), line "
                    + trace[0].getLineNumber());
        }
        return sb.toString();
    }
}