android - 如何获取发生它的异常的类和方法名称

时间:2011-04-01 12:32:43

标签: java android logging exception-handling

嘿伙计们,我想在我的Android应用程序中处理所有异常,这是应用程序类

我在我的应用程序类中有一个类似的方法:

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

现在缺少的是魔术方法,它使用发生异常的原始类和方法填充我的className和methodName变量

任何人都可以提供帮助

提前感谢...

1 个答案:

答案 0 :(得分:3)

使用e.getStackTrace (),使用您的包名称(使用StackTraceElement.getClassName())查看此数组搜索类名 - 这是您的类名“魔术方法”。然后使用StackTraceElement.getMethodName()获取方法名称。