indra
indra

Reputation: 832

how to catch the unknown exception in run time android

I have written some android application , while doing some testing it crashes .

but I dont know the reasin for crashing .. so I need to save the reason for crashing in some file so that later I can see that .. since I wont connect that to the PC while apllication is running , I need some suggestion to save the reason for crashing ..?

Upvotes: 2

Views: 1793

Answers (5)

Stefano
Stefano

Reputation: 3235

You need to use Application Class and set "setDefaultUncaughtExceptionHandler"

public class YourApplication extends Application {

private Thread.UncaughtExceptionHandler defaultUEH;
// handler listener
private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler =
        new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable ex) {

                // here I do logging of exception to a db
                PendingIntent myActivity = PendingIntent.getActivity(getApplicationContext(),
                        192837, new Intent(getApplicationContext(), YourActivity.class),
                        PendingIntent.FLAG_ONE_SHOT);

                AlarmManager alarmManager;
                alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                        15000, myActivity);
                System.exit(2);

                // re-throw critical exception further to the os (important)
                defaultUEH.uncaughtException(thread, ex);
            }
        };

public YourApplication() {
    defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
    // setup handler for uncaught exception
    Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
}

}

and set tag name="your.app.application.YourApplication" in application tag in the Android Manifest

Upvotes: 0

dragostis
dragostis

Reputation: 2662

You can do a try catch block:

try {
    //the code lies here
} catch(Exception e) {
    //here you can save the e.toString() to a FileOutputStream and then save the file
}

Upvotes: 0

Jon
Jon

Reputation: 3204

Sorry for the limited detail, but doesn't android already have an option for logging?

http://developer.android.com/reference/android/util/Log.html

I haven't tried this and would probably choose the try,catch answer but this is just another option.

Upvotes: 0

Raghav Sood
Raghav Sood

Reputation: 82563

The best method I've come across so far is to add the Internet permission, and use ACRA. ACRA allows you to send your reports to a Google Docs spreadsheet, and works for testing and production.

Other than ACRA, you have two options:

  1. Surround every bit of code that can throw an error at all with a try catch block and save the data somewhere.
  2. Thread.setDefaultUncaughtExceptionHandler() can be used to set a Handler that is called upon all unhandled exceptions.

Upvotes: 1

waqaslam
waqaslam

Reputation: 68187

Simply do it as below:

try{
    //your code which throws exception
}
catch(Exception e){
   e.printStackTrace();   //optional

   //save your exception in a text file on sd-card
}

Later you may read text files to get to know the exceptions

Upvotes: 1

Related Questions