find Method from Throwable - Java java.lang

Java examples for java.lang:Exception

Description

find Method from Throwable

Demo Code


//package com.java2s;

public class Main {
    /**//from w w w. j  a  va 2 s. c o m
     * @param t Throwable
     * @return String
     */
    public static String findMethod(Throwable t) {
        StackTraceElement ste = getStackTraceElement(t);
        return ste != null ? ste.getMethodName() : "none";
    }

    /**
     * @param t Throwable
     * @return StackTraceElement
     */
    public static StackTraceElement getStackTraceElement(Throwable t) {

        for (StackTraceElement ste : t.getStackTrace()) {
            if (!ste.isNativeMethod()) {
                return ste;
            }
        }
        return null;
    }
}

Related Tutorials