0

Most of you will probably label this is a very generic question, but still, I feel that I should make it:

What are the best practices while developing an android app so it can handle errors in a way that doesn't Force Close?

I'm asking this because I'm newbie developing android apps and I'm struggling with force closes a lot... I'm guessing that must be a way to design the APP so it can avoid force closes whenever a problem exists (ex:no internet) Right know I'm using try/catch but it doesn't seem a very programmatic and clean way of doing things, is there other options to handle exceptions besides try/catch ?

If you guys could send links to some good documentation or some sample code on this subject I would be very appreciated! tks!

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • http://developers.facebook.com/docs/mobile/android/build/ – Har Apr 20 '12 at 15:28
  • If you are getting force closes you are, likely, writing poor code. – dymmeh Apr 20 '12 at 15:28
  • 1
    Nope. Force close are quite helpfull and happen to good coders too. – Snicolas Apr 20 '12 at 15:30
  • @dymmeh: that's the exact reason why I'm here asking for some advises, isn't it ? – Pedro Lobito Apr 20 '12 at 15:30
  • Right. You get a force close and then fix the problem resulting in good code (generally). Aka, you originally wrote poor code. – dymmeh Apr 20 '12 at 15:31
  • 1
    @Tuga you are getting force closes because of something you wrote. Check logcat to see what is causing the force close. Trace back to the code that it points to (debug if needed). Correct the code to prevent the force close. try/catches are acceptable for certain things in terms of preventing full force closes. You shouldn't rely on them for everything. I'm not trying to be a smart-ass fyi. Your question just has an obvious answer: write better code. – dymmeh Apr 20 '12 at 15:32
  • Check my answer on [Important DONT's for a good android app design(Beginner/Novice Level)?](http://stackoverflow.com/a/9817898/892055) – Arif Nadeem Apr 20 '12 at 16:09

3 Answers3

3

Force Closes happen in cases of uncaught exceptions. You should be able to avoid them by good coding practices and, in general, cleaning up after yourself. If you have questions about a specific force close scenario, post a question about it and we'll see what we can do.

The very specific answer to your question (which is probably a very bad idea in this case) is that you can use a global default uncaught exception handler which will supersede the Google "force close" dialog. This is usually only a good idea if you have a very well-developed app and are guarding against rare errors in production or want to use some other logging/reporting functionality than the Google market default.

Note that this won't prevent exceptions; it will only pass them somewhere other than the default Google handler.

You may find some use in the Android Activity lifecycle for persisting data or looking at threading (which are two cases where you can get force closes if you don't have your head wrapped around it) if you're new to Android.

Community
  • 1
  • 1
Jon O
  • 6,532
  • 1
  • 46
  • 57
  • 1
    I second this answer too and how rare it should be to use a global exception handler. You are right it's actually a very bad advice to give to a java beginner and you signaled that well. – Snicolas Apr 20 '12 at 15:42
  • I've followed the link you've posted to http://stackoverflow.com/questions/2764394/ideal-way-to-set-global-uncaught-exception-handler-in-android but it doesn't seem to specify a way of implementing it, have you ever used the "global default uncaught exception handler" on your code, if so, do you have any good tutorial or sample code about this ? – Pedro Lobito Apr 20 '12 at 15:43
  • @Snicolas I'm aware that this isn't the path to choose, but it's also nice to know about it, thanks to both :-) – Pedro Lobito Apr 20 '12 at 15:45
  • 1
    @Tuga: "but it doesn't seem to specify a way of implementing it" -- yes, it does. "do you have any good tutorial on sample code on this ?" -- this has nothing to do with Android. `setDefaultUncaughtExceptionHandler()` is part of standard Java, and there are plenty of examples online if you would care to look. You might also examine the implementation of [ACRA](http://code.google.com/p/acra/) or other error-reporting components to see how they use it. – CommonsWare Apr 20 '12 at 15:45
  • 1
    There's a rough example in the zero-score answer here: http://stackoverflow.com/questions/4427515/using-global-exception-handling-on-android There are also some well-documented caveats in that thread. – Jon O Apr 20 '12 at 15:47
  • @JonO the example is handy, thanks. – Pedro Lobito Apr 20 '12 at 16:05
  • @CommonsWare I'll definitively take a look at ACRA, it seems a good way to log errors without depending on, in my case, Google Play,tks. – Pedro Lobito Apr 20 '12 at 16:08
1

Actually force close is just a symptom of something going really bad in your app. Try/catch is the good mechanism to use in java (and on android) to make your app more robust and declare some behavior in case of errors.

I disagree with other comments. It's quite hard to build apps and foresee every possible exceptions/errors that can occur. So don't worry about no getting force close, you should better focus on how you can read them, understand log cat traces and find out bugs that caused there force close.

Programming is error prone and difficult, and it has always been somewhat tinted by a process of trying and getting errors and correcting them. So don't get desperate, you are on the right track.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • You answer is helpful and confirms what I thought, but is there a good way to handle this errors all inside the same class, and just call the "error.class" whenever an exception occurs, instead of "surrounding" my code with try/catches ? I'm not sure if this makes sense to you, I hope it does ! – Pedro Lobito Apr 20 '12 at 15:35
  • 1
    Exception handling can be tricky to do in the right way. Usually, there are some design guidelines that you should follow to find out if you need to treat (catch) the errors locally or send them to the caller by declaring a throws clause for the method containg dangerous code. Try to google : java exception handling strategy or layer to read more. this page gives a good idea : http://www.goospoos.com/2010/02/ideal-way-of-exception-handling-in-multi-layer-java-web-application/ – Snicolas Apr 20 '12 at 15:40
1

This is a very broad question, as you said yourself. You should read 'Best practices' section at android developers site. Also for responsiveness see this page.

As for other way of handling exception rather than try/catch, bad news; there is none. BUT what you can do is check before using a resource for its availability. You can check for many things, including internet access, before you actually use it. I cannot list all of them here, but you will learn with time. This post will tell you how to check for internet availability.

Hope this helps.

Community
  • 1
  • 1
Hassan
  • 1,002
  • 15
  • 21
  • I'm reading "Designing for Responsiveness" http://developer.android.com/guide/practices/design/responsiveness.html, it's a must, tks for sharing. – Pedro Lobito Apr 20 '12 at 16:11