When an exception occurs, i try to report it to a log server.
i have caught an exception which inherited java.lang.RuntimeException when using Thread.setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler).
but i haven't caught any subclasses that have inherited java.lang.Error. (ex. OutOfMemoryError, IOError, ...)
so i use Runtime.addShutdownHook(Thread hookThread), but the hookThread.run() method is never called.
how do i catch an Error globally on android?
i do not want to use another process.. i just want to try to report a bug on the log server.
sorry evertyone, i try to catch error throwable continuedly, and i know java.lang.Error also is caught. but not run implemented function in UncaughtExceptionHandler.uncaughtException().
just.. printed log for checking.
i solve it. just i was mistake.
i handled Throwable object to obtained log information.
but i used Throwable.getCause() method, when occurred java.long.Error, it is null.
in uncaughtException() method, when occurred exeption, not print log exactly.
thank you, axis.
Related
When, for example, a NullPointerException occurs after calling the RxJava map operator, the app doesn't crash. I want the app to crash when this happens so it can submit reports to crashlytics etc.
I tried using Exceptions.propagate() in a try/catch block but that didn't work.
The only solution that I found was throwing a RuntimeException in my error handler.
override fun getCategories(): Single<List<Category>> {
return ApiManager
.getCategoriesService()
.getCategories()
.map { categoriesResponse ->
throw KotlinNullPointerException
categoriesResponse.categories?.map { categoryResponse ->
CategoryMapper().mapFromRemote(categoryResponse)
}
}
}
The NullPointerException thrown inside the map operator does not crash the app.
If I call it before the return statement it crashes the app.
If I call it before the return statement it crashes the app.
It crashes because getCategories method is running on Android's main thread to build the rx chain.
The NullPointerException thrown inside the map operator does not crash the app.
It doesn't crash the app because this chain is being subscribed to a thread that is not Android's main thread. E.g. your chain has .subscribeOn(Schedulers.io()).
The only solution that I found was throwing a RuntimeException in my error handler.
That's the expected design of your chain as per this document:
An Observable typically does not throw exceptions. Instead it notifies any observers that an unrecoverable error has occurred by terminating the Observable sequence with an onError notification.
So rather than catch exceptions, your observer or operator should more typically respond to onError notifications of exceptions.
Ok so I think the closest to what I want to achieve is by calling Exceptions.propagate(throwable) in the error handler called in onError.
Thanks for pointing me in the right direction #Gustavo #TooManyEduardos2
I had a bad crash case that was caused due to some Asyncs doing stuff in improper order in a SQLite and thing blew up. It took me some time to debug all that and access to the internal db would have helped immensely. I know how to access that internal db on a dev device but in case something goes wrong I would like to be able to get an instance of that db no matter the device. For error reporting I am using Crashlytics.
The question is: Is there a way to have Crashlytics run a piece of code (method, etc) during the crash collection/reporting? (For example, get db copy and email it, or something)
Couldn't find something in the documentation.
It is possible to get control prior to Crashlytics logging a crash. You essentially have to create your own uncaught exception handler and call Crashlytics' handler from there. Something like this in your Application class:
private UncaughtExceptionHandler originalUncaughtHandler;
#Override
public void onCreate() {
// initialize Fabric with Crashlytics
originalUncaughtHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
// do the rest of your oncreate stuff
}
#Override
public void uncaughtException(Thread thread, Throwable ex) {
// do your work to add data to Crashlytics log
originalUncaughtHandler.uncaughtException(thread, ex);
}
No you can't. You can however set certain values before initiating Crashlytics. Like adding values to parameters so as to identify user. Like adding email id of user before creating a crashlytics session.
As #basu-singh said, you can add context to the crash, see https://docs.fabric.io/android/crashlytics/enhanced-reports.html
Or you can use your own UncaughtExceptionHandler, and then call Crashlytics. Though your code needs to be extra safe !
I am looking at how to handle exceptions in Android.
In the update() function in the sample code for the Notepad Content Provider, it calls getWriteableDatabase(), which can potentially throw an SQLiteException.
I notice that the NoteEditor Activity saveNote() function has the following code:
// Commit all of our changes to persistent storage. When the update completes
// the content provider will notify the cursor of the change, which will
// cause the UI to be updated.
try {
getContentResolver().update(mUri, values, null, null);
} catch (NullPointerException e) {
Log.e(TAG, e.getMessage());
}
What happens if an SQLiteException occurs?. I want to be able to catch this exception in the Activity and display an appropriate message to the user (via a toast or something similar).
I thought I could do by adding an extra catch for SQLiteException. However, I read the following info in the Google docs:
"Remember that the Android system must be able to communicate the Exception across process boundaries. Android can do this for the following exceptions that may be useful in >handling query errors:
IllegalArgumentException (You may choose to throw this if your provider receives an >invalid content URI)
NullPointerException"
So I am now confused - can I catch the SQLiteException or not?
Whenever possible, you should catch the Exception in the class or component in which the Exception occurred. Use some sort of broadcast mechanism or return value semantics to report errors across processes.
It's admittedly a tricky situation. Some people say that if an SQLite (or other Exception) occurs in a ContentProvider, the provider should propagate the exception upwards instead of returning null in the Cursor. However, this generally won't work across processes! On the other hand, returning null doesn't give you a lot of information.
A limited set of Exceptions do traverse process boundaries, but SQLiteException isn't among them - still they might be useful/appropriate.
When trying to execute ShadowCanvas.getHeight(), I get a NullPointerException in ShadowWrangler.InvocationPlan.toString()
My app code:
public float getCanvasHeight() {
return mCanvas.getHeight();
}
Throws exception:
Method threw 'java.lang.NullPointerException' exception. Cannot
evaluate
com.xtremelabs.robolectric.bytecode.ShadowWrangler$InvocationPlan.toString()
Any ideas what I'm doing wrong?
I'm guessing that this shows up in your debugger but doesn't get thrown when you actually run the code?
The debugger probably calls toString() when showing the variables, and some value used in the implementation of toString() isn't yet initialized. So instead of showing you the string of the contents, it shows this exception.
Unless you actually call toString() in the code this exception wouldn't be thrown at runtime, although the uninitialized value could be an issue somewhere else.
i have a try/catch in a function that returns a value. if all goes well the return statement in the try block works fine. but what am i supposed to do if theres an exception? what do i return in the catch and finally blocks? the return statement has to be there or the code doesnt compile.
edit: in 1 function i connect to a URL, read a file, and return a string. in another function i open an image from the internet and return a bitmap. so in both of these cases, what am i supposed to have in the return statement at the catch and finally blocks?
One of the following:
Return a special value that indicates an error to the calling code.
Return a default value (depending on your context, there may not be a good one).
Don't catch the exception, instead add a throws to the header.
Catch the exception, do the cleanup, and rethrow the exception.
In general, there's no escaping the fact that the function can error out. The calling code must either be notified of that, or the function must effectively swallow an error and pretend nothing bad happened; that involves returning something. The specifics depend on your context...
The value you return should be able to represent an error, for example, null should mean the function didn't work. So, in the catch block, the function would return null, for example. In the finally block, you should free any resource you used (for example, close any files you opened, etc).
You put those things in the finally block because it's guaranteed that it will be ran sometime, even if the code in your catch block throws an unhandled exception or anything. And it will also run if the function worked just as wanted.
Use return null; this statement outside your try/catch block. If the things work, your try block will execute and will return , if it fails because of exception, it will be caught and you will see error.