In my android app I need a global exception handling mechanism. That is an exception handler which works when an exception occurs from any activity of my app. Is this possible? How?
if you have access to your own web server with PHP installed, you might have a look at this
http://code.google.com/p/android-remote-stacktrace/
The Google Groups thread they're pointing you to is a good reference. I have a helper class in my app that offers the user the option of sending a failure report to me with the details of the crash. Basically, when the failure is caught it writes the details out to the SD card and on next run of the app it displays a pop-up.
It won't keep your app from crashing, but it will let you do something intelligent with result.
Related
I've just started work with Koltin and my question might be a little strange to someone who have more experience,
but how can i see fatalExceptions in logcat? For example, i have an app that is already developed by another dev,
there is an error in one activity - after pressing the button apps crash and restart to main activity.
I don't see any usefull informations in logcat(in fabric also!), moving on trough whole code from listener to fragment and many classess is very time consuming. There must be some way to figure it out quicker, right?
Exceptions should be shown/thrown in logcat, same as with Java.
If the exception is thown within rxjava or a kotlin coroutine, make sure you have defined an error handler, otherwise the exception might get swallowed.
Then make sure you have selected the right app in logcat and that no filter is active.
Also make sure there is no other global Exception handler defined besides fabric.
I'm building an app that sometimes crashes, I want to know that it crashed in the next time I opened it so I can suggest to the user some post-crash options.
How can I detect the crash?
Also I want to be able to save the user's work before it crashes, I means real time detection of crash, can I do it without knowing where it crashed?
You will need to know where it crashed in order to set the try/catch blocks in the right place to, er, catch the crash and save the data or whatever you have in mind.
This is known as "graceful" termination if you want to consider it in more detail.
Unfortunately neither Java destructor/finalize methods nor lifecycle methods such as onDestroy are anywhere near as robust as try/catch blocks so I'm afraid that is your only option, and how many of us deal with exception prevention. No-one would wittingly provide a user experience that crashes, much less with loss of their data.
Take a took at the ACRA library. You can configure it so whenever a crash happens you can control it and even send the crash log by email
You can use try/catch blocks, then send details on the Exception in your catch.
There are implement UncaughtExceptionHandler as mentioned in these answers and write crash report in some file or use it another way.
ACRA is already mentioned.
However for paid version, I found BugSnag is very good at this.
Or if you want to take the extra mile, try AppSee.
AppSee even has video recording session of how the crash happens. It is from tapping that button on the second list, the menu button or even when the user slides left in your viewpager.
What is the usual way on Android to stop my application if it has reached an unrecoverable error.
finish() will not do it, since it wont stop any running services or threads. Furthermore I would like to inform the user what has happend and please him to send an error log.
As far as I googled, it seems like there is no way to close my application and open a special crashreport activity or something else to show the user whats going on or send a crash log.
I think you should throw unhandled runtime exception. In such case android will kill all your process. Also I suggest you to use ACRA. This library will help you to get crash report (via email, google docs, etс.) and it can show customizable error dialog to a user.
You should check this out. This could be your solution.
ACRA
Check the basic setup guide to start using the library. ACRA - Basic Setup
While ACRA is an okay solution, if you want to implement your own logging of unhandled exceptions try Thread.setDefaultUncaughtExceptionHandler(). That way you can get any exceptions that are thrown and not caught, and log them the way you like. You need to implement Thread.UncaughtExceptionHandler and pass it to that method.
With an Activity, it would look something like this in onCreate():
getMainLooper().getThread().setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
I am not able to handle all the exceptions in all activities of my package. But I'd like to have a one place (point) where I could handle anything, that extremely finishes the application and send error details to my server by email. Is there such approach? (E.g in WP7 there is private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) method
I use a variant of this unhandled exception handler to capture exceptions and log details back to a server.
I took this code and tweaked it:
http://code.google.com/p/android-remote-stacktrace/
Is there any way to catch crashes in my own android app? Something like a global uncaught exception handler? Just wondering if I could install something like that, then if an uncaught exception is thrown, I could pop up a dialog for the user and ask if they want to mail a dump of the exception to me.
Thanks
Is there any way to catch crashes in
my own android app? Something like a
global uncaught exception handler?
See Thread.setDefaultUncaughtExceptionHandler().
Just wondering if I could install
something like that, then if an
uncaught exception is thrown, I could
pop up a dialog for the user and ask
if they want to mail a dump of the
exception to me.
You can also use Flurry, DroidDrop, or any of the other implementations of this already available.
Bugsense.com also does this (for free)
Flurry does this, plus a lot of other useful things.
DroidDrop is a dead project. Neat Idea though wish they open sourced it.
FWIW, we've been using Crittercism's free plan with success. They also offer some premium features, like handling the exception so the app doesn't crash.
In the free version, the user still sees the crash, but at least I get the email and the stack trace.
We also use the iOS version (but I've heard from my colleagues that it is not quite as good).
Here are similar questions:
Is it possible to create some sort of global exception handler in Android?
Ideal way to set global uncaught exception Handler in Android