Using try/catch for preventing app from crashes

前端 未结 14 1843
你的背包
你的背包 2021-01-30 03:37

I have been working on an Android app which uses try/catch frequently to prevent it from crashing even on places where there is no need. For example,

A view

相关标签:
14条回答
  • 2021-01-30 04:30

    This is bad practice. Other answers have said that but I'd think it's important to step back and understand why we have exceptions in the first place.

    Every function has a post-condition – a set of things that must all be true after that function executes. For example, a function that reads from a file has the post condition that the data in the file will be read from disk and returned. An exception, then, is thrown when a function has not been able to satisfy one of its post-conditions.

    By ignoring an exception from a function (or even effectively ignoring it by simply logging the exception), you're saying that you're ok with that function not actually doing all the work it agreed to do. This seems unlikely – if a function does not run correctly, there is no guarantee that what follows will run at all. And if the rest of your code runs fine whether or not a particular function runs to completion, then one wonders why you have that function in the first place.

    [Now there are certain cases where empty catches are ok. For example, logging is something that you might justify wrapping in an empty catch. Your application will probably run fine even if some of the logging can't be written. But those are special cases that you have to work really hard to find in a normal app.]

    So the point is, this is bad practice because it doesn't actually keep your app running (the supposed justification for this style). Maybe technically the OS hasn't killed it. But it's unlikely that the app is still running properly after simply ignoring an exception. And in the worst case, it could actually be doing harm (e.g. corrupting user files, etc.).

    0 讨论(0)
  • Another perspective, as someone who writes enterprise software on a daily basis, if an app has an unrecoverable error, I want it to crash. Crashing is desirable. If it crashes, it gets logged. If it crashes more than a few times in a short period of time, I get an e-mail saying that the app is crashing and I can verify that our app and all the web services we consume are still working.

    So the question:

    Is

    try{
      someMethod();
    }catch(Exception e){}
    

    good practice? No! Here are a few points:

    1. The MOST important thing: This is a bad customer experience. How am I supposed to know when something bad is happening? My customers are trying to use my app and nothing works. They can't check their bank account, pay their bills, whatever my app does. My app is completely useless, but hey, at least it didn't crash! (Part of me believes this "Senior" dev gets brownie points for low crash numbers, so they're gaming the system.)

    2. When I'm doing development and I write bad code, if I'm just catching and swallowing all exceptions at the top layer I have no logging. I have nothing in my console, and my app fails silently. From what I can tell, everything appears to work okay. So I commit the code... Turns out my DAO object was null the whole time, and the customer's payments were never actually updating in the DB. Whoops! But my app didn't crash, so that's a plus.

    3. Playing devil's advocate, let's say I was okay with catching and swallowing every exception. It's extremely easy to write a custom exception handler in Android. If you really just have to catch every exception, you can do it in one place and not pepper try/catch all over your codebase.

    Some of the developers I've worked with in the past have thought crashing was bad.

    I have to assure them we want our app to crash. No, an unstable app is not okay, but a crash means we did something wrong and we need to fix it. The faster it crashes, the earlier we find it, the easier it is to fix. The only other option I can think of is allowing the user to continue in a broken session, which I equate with pissing off my userbase.

    0 讨论(0)
  • 2021-01-30 04:32

    From the Android documentation:

    Let's entitle it as -

    Don't Catch Generic Exception

    It can also be tempting to be lazy when catching exceptions and do something like this:

    try {
        someComplicatedIOFunction();        // may throw IOException
        someComplicatedParsingFunction();   // may throw ParsingException
        someComplicatedSecurityFunction();  // may throw SecurityException
        // phew, made it all the way
    } catch (Exception e) {                 // I'll just catch all exceptions
        handleError();                      // with one generic handler!
    }
    

    In almost all cases it is inappropriate to catch generic Exception or Throwable (preferably not Throwable because it includes Error exceptions). It is very dangerous because it means that Exceptions you never expected (including RuntimeExceptions like ClassCastException) get caught in application-level error handling.

    It obscures the failure handling properties of your code, meaning if someone adds a new type of Exception in the code you're calling, the compiler won't help you realize you need to handle the error differently.

    Alternatives to catching generic Exception:

    • Catch each exception separately as separate catch blocks after a single try. This can be awkward but is still preferable to catching all Exceptions.
      Edit by author: This one is my choice. Beware repeating too much code in the catch blocks. If you are using Java 7 or above, use multi-catch to avoid repeating the same catch block.
    • Refactor your code to have more fine-grained error handling, with multiple try blocks. Split up the IO from the parsing, handle errors separately in each case.
    • Re-throw the exception. Many times you don't need to catch the exception at this level anyway, just let the method throw it.

    In most cases you shouldn't be handling different types of exception the same way.

    Formatting / paragraphing slightly modified from the source for this answer.

    P.S. Don't be afraid of Exceptions!! They are friends!!!

    0 讨论(0)
  • 2021-01-30 04:34

    I have been developing android apps for the past 4-5 years and never used a try catch for view initialisation.

    If its a toolbar do like this

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    

    eg:- To get a TextView from a view(fragment/dialog/any custom view)

    TextView textview = (TextView) view.findViewById(R.id.viewId);
    

    TextView textview = (TextView) view.findViewById(R.id.viewId);

    instead of this

    View view = findViewById(R.id.toolbar);
    

    A view object has minimum reach compared to its real view type.

    Note:- May be it crashed because the view was loaded. But adding try catch is a bad practice.

    0 讨论(0)
  • 2021-01-30 04:35

    This is bad for multiple reasons:

    1. What are you doing that findViewById throws an Exception? Fix that (and tell me, because I've never seen this) instead of catching.
    2. Don't catch Exception when you could catch a specific type of exception.
    3. There is this belief that good apps don't crash. That's not true. A good app crashes if it must.

    If an app goes into a bad state, it is much better for it to crash than for it to chug along in its unusable state. When one sees an NPE, one shouldn't just stick in a null check and walk away. The better approach is to find out why something is null and either stop it from being null, or (if null ends up being a valid and expected state) check for null. But you have to understand why the issue occurs in the first place.

    0 讨论(0)
  • 2021-01-30 04:37

    We pretty use much your same logic. Use try-catch to prevent production apps from crashing.

    Exceptions should be NEVER ignored. It is a bad coding practice. The guys maintaining the code will have a really hard time localizing the part of code that raised the exception if they are not logged.

    We use Crashlytics to log the exceptions. The code will not crash (but some functionality will be disrupted). But you get the exception log in the dashboard of Fabric/Crashlytics. You can look at these logs and fix the exceptions.

    try {
        codeThatCouldRaiseError();
    } catch (Exception e) {
        e.printStackTrace();
        Crashlytics.logException(e);
    }
    
    0 讨论(0)
提交回复
热议问题