I am working on an app with remote exception-handling. That is, if an exception happens somewhere that wasn't expected, the app closes gracefully and a report is generated in the log on our server. I am using the UncaughtExceptionHandler interface for this and it works well except that it does not seem to catch Throwables that are actually Errors.
Obviously, the best solution is to just handle everything and/or not allow anything to throw an Error in the first place. For the sake of thorough error-handling, however, I would like to be able to catch unexpected Errors as well as Exceptions. I was unable to find anything like an UncaughtErrorHandler in Android, does anyone know if such a thing is possible?
There is generally no way to catch Errors: typically once an Error happens you can't actually intercept it. As far as I know there is no fully-supported mechanism in the Java language to catch these, and there really isn't any one on Android either.
You might want to check out several SDKs like Crittercism. A rudimentary way to achieve what you want is to use Thread.setDefaultUncaughtExceptionHandler().
Related
So I have an android application that has a myriad of activities (intents?) which all do their own thing. We were wanting to create a sort of debug log though, one where you can see what the user has been through and error that occurs. You may have had to send one before if you've run into a bug with a program you've used.
My initial thoughts are to just create a class where I can send information/data to and it just writes it onto a text file. It would need to be accessible across all the activities so that I can easily write to it and re-use it.
I do wonder whether that's a good way to go through, noted that it doesn't really save any actual errors but only data I tell it to. And I'm not sure if its a great idea to be constantly opening->writing->closing a file for a debug log.
Is there a smarter way? Or a common pattern that would be good to use?
Thanks so much!
Sentry's Android SDK, will automatically report errors and exceptions in your application.
The Sentry SDK catches the exception right before the crash and builds a crash report that will persist to the disk. The SDK will try to send the report right after the crash, but since the environment may be unstable at the crash time, the report is guaranteed to send once the application is started again.
You can see full documentation here
We are working on an Android app. We are using the Chilkat library to handle the email communication and it works well. Although, there may be a "issue" involving some JNI communication. From internet research and discussing with Chilkat, the problem may involve how EMOJI's are processed in messages.
Periodically, we get an error that hard crashes the app and it comes from the library call. It is a single line call that sometimes crashes depending on something within the message itself. Our call is...
tmpstr=email.getHtmlBody();
The hard crash error we get is:
JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0xf0
Again, doing some internet searches on this message... this apparently is a more common issue than anyone wants but still needs to be addressed. Chilkat is working on a fix but in the meantime, we need to continue with the app.
What I am trying to figure out if there is a way that we can catch the error and prevent the app from crashing... maybe just flagging this message and continuing with the next message?
Seeing how I have never tried to catch an error from a 3rd party library, I am not sure where to go with this.
Any suggestions or examples that might help would be greatly appreciated.
Well, depending on error, you can always try to "fix" it. Working with legacy code is always hard and all you can do here are some hacks.
You can do few things here:
try to catch signals
make sure your app doesn't exit your JVM
Take a look here: http://jnicookbook.owsiak.org/recipe-No-015/ and here: http://jnicookbook.owsiak.org/recipe-No-016/
Maybe you will find some solution based on these samples.
I know there is "try" and "catch" for specific parts of code, that if they fail it goes in catch and you can display the crash exception. But is there a way to do it over all the code in case of a crash?
Recently I have posted a beta verison of an app game I made, and some users reported random crashes which never occurred on my device, I really want to know what causes those crashes but it is a problem asking each one of them to debug it and check logs. So I thought of displaying a toast with the exception when it happens, but I don't know if it is possible and if it is how to do it.
I advise you to check out "Beta by Crashlytics" and "Crashlytics": https://get.fabric.io/. Both are frameworks that are extremely easy to set up and use. You use the first one in beta stages of your app, and the other one when your app is live.
There is a great deal of difference between could and should. For instance,
public static void main(String args[]){
try{
} catch (Exception e){
}
}
will compile. It will accept any exception heading its way. However, you should not do this. Exceptions are good for programming since they let you know where you went wrong and how to correct it. You should work on correctly testing your code as opposed to finding out ways to make sure what you wrote doesn't tell you its broken. I remember working on this and finding it very frustrating when I was beginning. Believe me it is better this way. I hope this helps.
I would like to add a feature of restarting from crash for my applications. So, I need to simulate the crash abort. It should be easy but I just can't make it crash itself.
It's a phonegap application, and the best would be crashed from the javascript in html. But no matter I tried, the application does not crash, the application is just hang there and not responding any clicks. I've tried to write some codes in the java phonegap plugins, however, I can't make it crash as well. Any suggestion?
Try something like this:
Object obj=null;
obj.toString();
This should cause a NullPointerException. When you don't catch it anywhere you should be able to test your recovery functionality. But think about a possibility to get bugreports from your users - elsewhere you will never be able to fix ugly bugs that happen only on other peoples devices.
Try the following:
Create a HelloWorld application.
Add a Log statement to the end of onCreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("HelloWorldActivity.onCreate()", "setContentView() completed");
}
Place a breakpoint on the Log statement.
Run the app in the emulator and note it works and step to see the Logged entry in the LogCat window of Eclipse.
Change the HelloWorldActivity to extend from ListActivity instead of Activity.
public class HelloWorldActivity extends ListActivity {
Run the app in the emulator again and note it fails to reach the Log statement.
My question is NOT why this fails. My question is, how would you go about debugging this failure? All I see in the Eclipse Debug pane is a RuntimeException. I see LogCat has a bunch of messages, but it's huge and I've searched it but can't find anything to indicate what's wrong or where in my code the exception happened. I can't find a way to display the message inside the RuntimeException or a stack trace to know which line of code initiated the exception.
I assume there must be better ways to use the tools to find errors, but I'm new and can't seem to figure out a better way to debug besides wrapping everything I code in a try/catch. I would have expected to find a message in LogCat generated by the throwing of the exception. I would have expected the Debug window to allow you to inspect the exception's contents. I'm not saying that such techniques don't exist, I'm saying I'm having trouble figuring out as a beginner how to debug and asking what techniques do exist and how do I use them?
So, simply put:
How would you find this error if you didn't already know what was causing it?
What techniques would you use to find out the root cause?
How would you go about inspecting the Exception's details?
Generally, how do you find problems in your Android code using Eclipse?
Multiple suggestions and discussion are welcomed. :)
I would have included my LogCat contents, but it's so large that's not reasonable. You should be able to easily reproduce this yourself, so I left it out. It is possible something is in LogCat to help me, but because it's so large with even running a small program, I would need a hint as to what to search for and how to interpret it when hitting an exception thrown from an API call. I see other posts that state something should be in LogCat, which while might be true, I'm not finding anything myself. If you think something should be in LogCat, please run the test yourself and copy the lines into your response that I should be finding.
Thanks.
========
Summary techniques list so far is as follows:
Invasive Techniques:
1. Place a Toast in code locations where you want to see you you've executed.
2. Place try/catch around code where you think there's a possibility of an Exception being thrown.
3. Comment out code and recompile and retest.
Non-Invasive Techniques:
1. Use the debugger. Breakpoints, variable inspection...
2. Monkey stress tester.
3. Download Android source library.
4. Use LogCat filters to see if a "Caused By" is listed.
Unclear if Available:
1. Debug version of Android library that has additional logging, assertions or other additional help.
2. Ability to inspect an Exception in Eclipse through the Debug pane or other techniques.
3. A way to define a more global try/catch exception handler.
4. Ability to debug through the Android library source code.
Not Available:
1. A non-invasive way to view the contents of an Exception or where the Exception happened.
hey,
Ineresting question. Well, first tip, you can filter what logcat tells you. For instance, you make it just show you errors by clickin in the red (e).
It also tells you where the error happened if you run your app in debug mode. It can either point you directly to your code or to android sdk. Knowing what android package caused the error is a big help.
These two just pop into my mind. hope it helps!
I was running into the same issue and found the following by Steve H. that helped out:
What happens is that when the debugger
is attached, the exception logs don't
get posted to LogCat until you
terminate the application from within
the Debug perspective. This happens
because the application doesn't
actually crash until the debugger
detaches. – Steve H Mar 31 at 15:47
------ yup, that did it. Now I see the same exception. AFTER I let the
program run through it's full crash
and exit process. It should display
that info when it halts my program and
brings up the IDE debugger screens.
Not leave me wondering and wasting my
time with more clicking around.
Eclipse has a long way to go it seems
to compete with the likes of Visual
Studio. Let's hope my patience for it
outlasts my project. Thanks for the
feedback. :) – Sebastian Dwornik Mar
31 at 17:35
Link to Question: What's wrong with debugging in Eclipse on Android?
Generally if anything throws an exception then you should probably be catering for that situation anyway, however putting try/catch blocks is a decent way of finding the specific problem.
I've found that if you don't put something in the catch block then you can't evaluate the exception in the watch variables window in eclipse. So i always put a Log call in and set a break point on that line.
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}catch(Exception exception)
{
// put break point on line below so you can evaluate exception in debug mode.
Log.e(TAG, "Set content exception "+ exception.getMessage());
// note some exceptions return null on getMessage();
}
Log.d("HelloWorldActivity.onCreate()", "setContentView() completed");
}
So if your stack trace is huge then this will help. Other developers have also found that getting the source code for the sdk means you can view where the error is thrown in the main sdk code. I've not done this though.
Exceptions not caught with try/catch are errors and break the normal flow of the program.
Running in debug mode is just clicking in the bug button. I don't know if there are any "special" debug libraries. But when working with android, all "libraries" are open source so you can pretty much view anything.
The good thing about debug mode is that when an error occurs, your app is frozen right in the limbo when the error occurs. You can set set breakpoints, change your code on the fly while your program is running, which is great (Well, you can't make drastic changes like changing a method name).
The way you treat bugs and errors in android, however, can be a bit different from .NET, since the model in each one is different.
When programming to windows, apps work like small islands. You have a much direct control over the code flow (ie: you can call a modal dialog to freeze codeflow while a user inputs some data) and you can make a totally functional program using just one thread. In android almost everything runs in it's own sync. And your app must be prepared to handle stuff like receiving a phone call in the middle of execution. So, you can apply this model to debugging also: Errors (that happen due to unforsen circumstances) tend to propagate much more than in other development ambient. The way those errors are handled is different too: this is apparent when you realize that your app still runs even after throwing an exception.
Some more useful tips:
You have a very powerful tool called Monkey, a stress tool that generates "pseudo-random streams of user events such as clicks, touches, or gestures, as well as a number of system-
level events".
LogCat indicates you the "cause" of the error. The line usually starts with Caused By. If you're interested in the cause rather than the consequence, you can further filter your error reports looking for "Caused by".
Last but not least, I find the old method of commenting lines and see what happens very useful to figure things out.
hope it helps