Is there any aproach to handle exception on Application level? Android - android

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/

Related

Send report after Android JNI signal/exception

I am trying to get firebase working with my Android app but it is mainly C++ code.
A lot of chances are that if there is any crash it will be some kind of bad access in the C++ part.
Firebase works well with uncaught java exceptions however I cannot get it to work with JNI signals/exceptions.
As far as I know it is not yet compatible with JNI but I thought a workaround would be something similar to this:
Somewehre in the C++ add a signal handler for signals we would like to handle that will send it back to the Java side and try to send a report ( with part of the stacktrace if possible ).
#include <cisgnal>
namespace
{
void SignalHandler( int sig )
{
// Code to call a static method in my Activity
}
}
CrashReporter::CrashReporter()
{
::signal( SIGABRT, & ::SignalHandler )
}
// In java
public static void SendReportOnCrash()
{
FirebaseCrash.report( new Exception( "OOPS" ) );
}
Unfortunately, fake reports are never sent, however I do get callback in Java.
I tried to launch a process separated activity in which I would call FirebaseCrash.report() but there is no non-static way to it therefore it always crash since FirebaseApp/Crash are not instantiated in secondary activity.
I come here to ask if someone would have a hint on how to do that.
My last try but least wanted test would be to write the stack trace to a file, and upon a new start, test if this file exists, if so use FirebaseCrash.log then send a fake report...
You are not guaranteed to be able to do any Java processing after the JVM calls abort() for a fatal error. Per the Java documentation:
SIGABRT
The HotSpot VM does not handle this signal. Instead it calls the abort
function after fatal error handling. If an application uses this
signal then it should terminate the process to preserve the expected
semantics.
Yes, that is for the Oracle implementation. It quite likely applies to all other implementations also.
Because at the point it calls abort(), the JVM expects to be killed.

Xamarin android - Integration with crittercism for unhandled exceptions not working

I've created a bindings project, hooked everything up in my onCreate etc. Everything works except for unhandled exceptions. Let me elaborate on "Everything works" - I can see via crittercism's live stats page that there is indeed an app load, I can also send up "ManagedExceptions" using the "LogHandledException" interface.
I have implemented the ICritterCallback interface and the "CrashedOnLastLoad" boolean is always false. This is wierd cos I can see the app crashes.
I have used these 3 ways to try and get my logs sent to Crittercism. (All 3 crash the app)
Java exception
throw new Java.Lang.IllegalArgumentException("This is a test for critter");
Background exception
.Click += delegate { ThreadPool.QueueUserWorkItem(o => { throw new Exception("Crashed Background thread."); } ); };
Simple .net exception
throw new Exception("Crashed UI thread.");
None of the above are registering as crashes on the next load... weird right?
Maybe the .net runtime is swallowing all the unhandled exceptions then calling exit gracefully on dalvic's runtime... is this possible..?
As a hack for now im implementing the exception handlers for android as per this blog post then calling Crittercism.LogHandledException(Throwable.FromException(e.Exception)); from inside both the events.
It works, but im using Crittercism's handled exceptions for unhandled exceptions.. So when I want to send up real "HandledExceptions" they will be lost in the mess.
So is there any way to send an unhandled exception to Crittercism??
Or is there a way to simulate a crash on android from .net that will send it to Crittercism??
Any help would be much appreciated!
Cheers,
Sam
Co-founder of Crittercism here. We just released an official plugin for Xamarin which should automatically log javascript exceptions (and any other crashes) as unhandled exceptions so you won't run into this problem anymore. You can download the latest version from the Xamarin asset store here:
http://components.xamarin.com/view/crittercism

Android: Usual way to stop and log an unrecoverable error

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());

Android. Exception handling

I would like to be able to determine, in case an exception occurs while the user is using my application, where exactly the exception took place. I'd like to do something similar ti printStackTrace() method. (So this is during build mode, not debug mode )
Currently I've put almost all my methods from all my classes inside a try-catch statement (each method has a try-catch statement which encompasses all it's instructions) and i can, at this point, display the "tree" or stack of methods if an exception occurs. But is there a way to determine either a line number of something to more precisely indicate where inside the method the exception occurred? Similar to what is displayed when you use printStackTrace().
I'm not really used with Exception handling, what is the best practice for doing this and can in be done?
EDIT
And one other thing. When i use printStackTrace() during build mode, where does it display the content, because Logcat isn't available? Can i retrieve that information and maybe do something with it?
OR
Even better, can i use getStackTrace() during build mode and convert the stuff there in String and maybe output it somewhere?
All the exceptions that are not handled by your code and make your app crash in release mode will appear in the android developper console, close to your app.
For this to work, you will need to retrace obfuscated stack traces.
About exception handling : I suggest you read this for instance. You are making a mistake about exception handling if you surround all your code by a try/catch block.
Exception handling is more subtile than that and is often influenced by design considerations (whether to treat exceptions locally or throw them back to the caller).
To sum up : in the core of your app : don't treat exception but throw them or let them be thrown, using the throws clause of your methods signatures. In the upper layers, closer to the UI, treat exceptions with try/catch and if an error occurs, make sure your app is in a stable state and display some usefull messages to users.
More details (but not that much) :
in the database layer : throw exception. You can still catch them to log them, but throw or rethrow them to tell caller that something went wrong.
in the business layer : catch them, make sure your business/domain model is in a stable state and recovers from the error, and throw them back to the caller.
in the UI layer : catch the exceptions and display some messages to users.

A common exception handler from all activities in android

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.

Categories

Resources