find Class Name from Throwable - Java java.lang

Java examples for java.lang:Exception

Description

find Class Name from Throwable

Demo Code


//package com.java2s;

public class Main {
    /**/*w ww. j a v  a2  s. c o  m*/
     * @param t Throwable
     * @return String
     */
    public static String findClassName(Throwable t) {
        StackTraceElement ste = getStackTraceElement(t);
        return ste != null ? ste.getClassName() : "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