Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get more details about an exception that occurred in my application?

I want to show details of an exception that occurred in my application, like the line number of the exception. Is this possible?

like image 664
pick Avatar asked Feb 20 '23 04:02

pick


1 Answers

Yes. There are lots of information about your exception in stackTraceElement in the following example.

customize this method:

private static String getExceptionDetails(Activity act, Exception e) {
    StackTraceElement[] stackTraceElement = e.getStackTrace();

    String fileName = "";
    String methodName = "";
    int lineNumber = 0;

    try {
        String packageName = act.getApplicationInfo().packageName;
        for (int i = 0; i < stackTraceElement.length; i++) {
            if (stackTraceElement[i].getClassName().startsWith(packageName))
            {
                fileName = stackTraceElement[i].getFileName();
                methodName = stackTraceElement[i].getMethodName();
                lineNumber = stackTraceElement[i].getLineNumber();
                break;
            }
        }
    } catch (Exception e2) {
    }

    return fileName + ":" + methodName + "():line "
            + String.valueOf(lineNumber);
}
like image 115
Bobs Avatar answered Apr 06 '23 15:04

Bobs