문제

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

도움이 되었습니까?

해결책

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);
}

다른 팁

Did you check?

exception.printStackTrace()
exception.printStackTrace()

Shows line number and method name which has generated exception.

Use try catch block as

 try{

   // here add your code 

    }catch(Exception e){
     Log.e("error",e.toString());
  }

you may try this:

public static String getStackTrace(Throwable throwable) {
  StringWriter sw = new StringWriter();

  try (PrintWriter pw = new PrintWriter(sw)) {
    throwable.printStackTrace(pw);
    return sw.toString();
  }
}

check -

exception.getMessage()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top