Registering own Handler for unexpected exceptions

suggest change

This is how you can react to exceptions which have not been catched, similar to the system’s standard “Application XYZ has crashed”

import android.app.Application;
import android.util.Log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Application class writing unexpected exceptions to a crash file before crashing.
 */
public class MyApplication extends Application {

private static final String TAG = “ExceptionHandler”;

@Override public void onCreate() { super.onCreate();

// Setup handler for uncaught exceptions.
final Thread.UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread thread, Throwable e) {
        try {
            handleUncaughtException(e);
            System.exit(1);
        } catch (Throwable e2) {
            Log.e(TAG, "Exception in custom exception handler", e2);
            defaultHandler.uncaughtException(thread, e);
        }
    }
});

}

private void handleUncaughtException(Throwable e) throws IOException { Log.e(TAG, “Uncaught exception logged to local file”, e);

// Create a new unique file
final DateFormat dateFormat =  new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US);
String timestamp;
File file = null;
while (file == null || file.exists()) {
    timestamp = dateFormat.format(new Date());
    file = new File(getFilesDir(), "crashLog_" + timestamp + ".txt");
}
Log.i(TAG, "Trying to create log file " + file.getPath());
file.createNewFile();

// Write the stacktrace to the file
FileWriter writer = null;
try {
    writer = new FileWriter(file, true);
    for (StackTraceElement element : e.getStackTrace()) {
        writer.write(element.toString());
    }
} finally {
    if (writer != null) writer.close();
}

// You can (and probably should) also display a dialog to notify the user

}

}

Then register this Application class in your AndroidManifest.xml:

<application android:name="de.ioxp.arkmobile.MyApplication" >

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Exceptions:
* Registering own Handler for unexpected exceptions

Table Of Contents
2 Gradle
5 Intent
10 Exceptions
17 Service
19 WebView
31 SQLite
35 Glide
37 Dialog
38 ACRA
44 Handler
53 Toast
63 Menu
65 Picasso
70 Volley
71 Widgets
78 Realm
90 Spinner
95 OkHttp
108 TextView
109 ListView
111 Loader
118 Xposed
119 Security
121 ImageView
123 Doze Mode
130 Drawables
131 Colors
134 Fresco
139 AdMob
145 Keyboard
146 Button
150 EditText
155 Vk SDK
163 ExoPlayer
169 XMPP
175 OpenCV
177 Threads
184 ORMLite
186 TabLayout
190 LruCache
192 Zip files
194 Fastlane
199 FileIO
202 Moshi
210 VideoView
216 Paint
218 ProGuard
226 CleverTap
228 ADB shell
229 Ping ICMP
230 AIDL
234 Context
240 JCodec
242 Okio
249 FuseView
254 Looper
261 Fastjson
263 Jackson
267 Smartcard