当前位置: 动力学知识库 > 问答 > 编程问答 >

java - android - how to get an exception's class and method name within which it occurred

问题描述:

hey guys i want to handle all exceptions within my android app in a central place, that is the application class

i have a method like below in my application class:

public void HandleException(Exception e)

{

String appName = getString(R.string.app_name);

String className = ""; // magic method to find out the original class name where the exception occurred

String methodName = ""; // magic method to find out the original method name where the exception occurred

Log.e(appName, className + " : " + methodName + " - " + e.getMessage());

}

all that is missing now is the magic method which populates my className and methodName variables with the original class and method within which the exception occurred

can anyone help

thanks in advance...

网友答案:

Use e.getStackTrace (), go through this array searching classname with your package name (using StackTraceElement.getClassName()) - here's your classname "magic method". Then get the method name with StackTraceElement.getMethodName().

分享给朋友:
您可能感兴趣的文章:
随机阅读: