Is it possible to incorporate custom UncaughtExceptionHandler along with crashlytics in one application? If yes - how?
-
any luck with this one? i also encountered the same issue with new relic. found out that the code was not able to chain the uncaught exception further. – Seph Remotigue Oct 13 '14 at 15:02
-
@SephRemotigue try this solution https://stackoverflow.com/a/56990688/6445611 – Александр Кундрюков Jul 11 '19 at 13:57
7 Answers
UPDATE
Please see @kmityak answer as Crashlytics/Fabric initialization is now asynchronous and my solution below is no longer valid.
ORIGINAL ANSWER
You can set your custom UncaughtExceptionHandler providing that it will pass exception to default UncaughtExceptionHandler to be handled later via Crashlytics.
Below code is implemented inside Application subclass:
private static Thread.UncaughtExceptionHandler mDefaultUEH;
private static Thread.UncaughtExceptionHandler mCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// Custom logic goes here
// This will make Crashlytics do its job
mDefaultUEH.uncaughtException(thread, ex);
}
};
@Override
public void onCreate() {
super.onCreate();
// Order is important!
// First, start Crashlytics
Crashlytics.start(this);
// Second, set custom UncaughtExceptionHandler
mDefaultUEH = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(mCaughtExceptionHandler);
}
Second option is to register Crashlytics after setting your custom UncaughtExceptionHandler - then all uncaught exceptions will be reported by Crashlytics as fatals, and after that passed to your custom handler.

- 1,635
- 1
- 14
- 22
-
3I am not getting method `Crashlytics.start(this)`. I'm using `crashlytics:2.5.1`. I just want to exit my app as well as send report to Fabric. How can I do so ? – SweetWisher ツ Aug 31 '15 at 09:22
-
1use `Fabric.with(this, Crashlytics())` instead of `Crashlytics.start(this)` – Tej Sep 06 '17 at 21:05
-
Could you please tell me, where exactly the code belongs? I don't understand what's meant by `... inside Application subclass`. Thank you! – Mr. B. Oct 12 '17 at 20:50
-
1You need to create a custom Applicaiton class. You can do that by creating a subclass of android.app.Application and register it in AndroidManifest.xml. More information here: https://developer.android.com/reference/android/app/Application.html – jskierbi Oct 16 '17 at 10:58
Since recent versions of Crashlytics perform initialization asynchronously, it's better to use Fabric's initialization callback:
private static Thread.UncaughtExceptionHandler mDefaultUEH;
private static Thread.UncaughtExceptionHandler mCaughtExceptionHandler =
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// Custom logic goes here
// This will make Crashlytics do its job
mDefaultUEH.uncaughtException(thread, ex);
}
};
CrashlyticsCore core = new CrashlyticsCore.Builder()
.disabled(BuildConfig.DEBUG)
.build();
Fabric.with(new Fabric.Builder(this).kits(new Crashlytics.Builder()
.core(core)
.build())
.initializationCallback(new InitializationCallback<Fabric>() {
@Override
public void success(Fabric fabric) {
mDefaultUEH = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(mCaughtExceptionHandler);
}
@Override
public void failure(Exception e) {
}
})
.build());
-
2From what I see in CrashlyticsCore (v2.9.4), CrashlyticsController.enableExceptionHandling() is called inside onPreExecute(). So this is still being done in the same thread. Also CrashlyticsUncaughtExceptionHandler (which the controller uses) makes sure it passes the exception to whatever default handler existed. So the order of initializing Fabric and your own exception handler does not matter as long as you do the same. – black Aug 06 '18 at 09:44
-
3I tried this in my Application class, in my case failure(Exception e) receives a callback with exception : "com.crashlytics.sdk.android.crashlytics-core Initialization was cancelled". Any ideas, why this would be happening ? – Rahul Gurnani Oct 10 '18 at 06:03
-
2I'm trying this, but the InitializationCallback does not get called. Any ideas? – Blitz Jan 16 '19 at 09:15
Yes, it is possible.
In your Application class:
@Override
public void onCreate() {
super.onCreate();
Crashlytics.start(this);
initUncaughtExceptionHandler();
}
private void initUncaughtExceptionHandler() {
final ScheduledThreadPoolExecutor c = new ScheduledThreadPoolExecutor(1);
c.schedule(new Runnable() {
@Override
public void run() {
final UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
// do my amazing stuff here
System.err.println("Error!");
//then pass the job to the previous handler
defaultHandler.uncaughtException(paramThread, paramThrowable);
}
});
}
}, 5, TimeUnit.SECONDS);
}
The reason I'm scheduling this after 5 seconds is because Crashlytics needs some time to set up his stuff. I'm using this code and it works perfectly. Of course if your app crashes on start, sorry but no custom handler ;)

- 3,427
- 4
- 36
- 57
None if those solutions worked for me. I did it like following:
// Setup handler for uncaught exceptions.
Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
{
@Override
public void uncaughtException (Thread thread, Throwable e)
{
//Send Report to Crashlytics. Crashlytics will send it as soon as it starts to work
Crashlytics.logException(e);
//Your custom codes to Restart the app or handle this crash
HandleCrashes(thread, e);
}
});
And here is my Custom Method to restart the APP:
private void HandleCrashes(Thread t, Throwable e) {
Intent i = new Intent(mContext, frmLogin.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("JustLogin", true);
startActivity(i);
System.exit(1);
}

- 189
- 4
-
4The problem of this solution is that Crashlytics will report this crash as Non-Fatal and not general crash. – Vito Valov Jul 04 '15 at 23:58
-
2@VitoValov is there any way around this? still send as fatal crash and also restart the app? – Guillaume Jul 04 '17 at 14:22
Turn off automatic collection
add this to your
AndroidManifest.xml
<meta-data
android:name="firebase_crashlytics_collection_enabled"
android:value="false" />
- Register your custome
UncaughtExceptionHandler
Thread.setDefaultUncaughtExceptionHandler(customerUncaughtExceptionHandler)
- Manually start Crashlytics after registered UncaughtExceptionHandler
Fabric.with(this, new Crashlytics());
- rebuild and reinstall your Application

- 71
- 3
I found a solution for Fabric 2.10.1:
Thread.setDefaultUncaughtExceptionHandler(yourExceptionHandler)
Fabric.with(this, Crashlytics())

- 354
- 4
- 12
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
// Delegate to the default uncaught exception handler provided by the system
Thread.getDefaultUncaughtExceptionHandler().uncaughtException(thread, throwable);
}
});
// Initialize Firebase Crashlytics
FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true);
// Set Firebase Crashlytics as the default uncaught exception handler
Thread.setDefaultUncaughtExceptionHandler(FirebaseCrashlytics.getInstance().getUncaughtExceptionHandler());

- 375
- 3
- 22
-
1What FirebaseCrashlytics API version has `.getUncaughtExceptionHandler()`? – Chisko Jun 01 '23 at 20:02