Raj
Raj

Reputation: 6830

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...

Upvotes: 2

Views: 4552

Answers (1)

ernazm
ernazm

Reputation: 9268

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().

Upvotes: 3

Related Questions