1

I have a big Android application and I have a requirement to log all types of errors that could happen in my Application including "Out of memory" etc. Is it possible to accomplish?

I can use

try {

} catch(throwable t) {

}

but adding a whole code to try/catch sounds bad. And how can I catch an errors like "out of memory"?

Ron Dadon
  • 2,666
  • 1
  • 13
  • 27
Rainmaker
  • 10,294
  • 9
  • 54
  • 89
  • If it is out-of-memory how does Android throws an Exception? ;) – Enzokie Jan 26 '17 at 06:22
  • I guess there is no way ) – Rainmaker Jan 26 '17 at 06:27
  • Actually my first comment is sarcasm. Btw all necessary Exceptions are defined in the Class/Methods therefore you know what to catch. And yeah using a generic exception e.g. `catch(Exception e)` is a bad idea. – Enzokie Jan 26 '17 at 06:31

3 Answers3

4

To tracking crash reports best way is using 3rd party libraries like Fabric or firebase(google). If you cannot use like this libraries, you can try this.

public class MyActivity extends Activity implements Thread.UncaughtExceptionHandler{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        if(ex.getClass().equals(OutOfMemoryError.class))
        {
            try {
                android.os.Debug.dumpHprofData(fileName);
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        ex.printStackTrace();
    }
}
Muhammed GÜNEŞ
  • 304
  • 2
  • 15
1

yes exactly it is possible. You can write a parent activity and handle exceptions globally like in this example. After that You can use Google analytics, Crashlytics to view your daily reports of crashes etc

elcuco
  • 8,948
  • 9
  • 47
  • 69
Junaid Hafeez
  • 1,618
  • 1
  • 16
  • 25
  • So I need to create a parent to each activity and it will catch all types of exceptions? Can I do it once in Application class for example? – Rainmaker Jan 26 '17 at 06:37
  • possible in application class, or you need only one parent activity, all other activities in your app would be inherited from that parent or base activity. – Junaid Hafeez Jan 26 '17 at 06:38
0

Best way is to include analytics.

please refer this. crashlytics is a library that can notify almost all problems. Try it.

Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57