find Line Number from Throwable - Java java.lang

Java examples for java.lang:Exception

Description

find Line Number from Throwable

Demo Code


//package com.java2s;

public class Main {
    private static final int NO_LINE_NUMBER = -1;

    /**// w w  w . j  av a  2s .  c o  m
     * @param t Throwable
     * @return int
     */
    public static int findLineNumber(Throwable t) {
        StackTraceElement ste = getStackTraceElement(t);
        return ste != null ? ste.getLineNumber() : NO_LINE_NUMBER;
    }

    /**
     * @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