title: Using try/catch for preventing app from crashes
body: <p>I have been working on an Android app which uses <code>try/catch</code> frequently to prevent it from crashing even on places where there is no need. For example,</p>
  
  <p>A view in <code>xml layout</code> with <code>id = toolbar</code> is referenced like:</p>
  
  <pre><code>// see new example below, this one is just confusing
  // it seems like I am asking about empty try/catch
  try {
      View view = findViewById(R.id.toolbar);
  }
  catch(Exception e) {
  }
  </code></pre>
  
  <p>This approach is used throughout the app. The stack trace is not printed and it's really hard to find what went wrong. The app closes suddenly without printing any stack trace.</p>
  
  <p>I asked my senior to explain it to me and he said,</p>
  
  <blockquote>
    <p>This is for preventing crashing in production.</p>
  </blockquote>
  
  <p><strong>I totally disagree with it</strong>. To me this is not the way to prevent apps from crashes. It shows that developer <strong>doesn't</strong> know what he/she is doing and is in doubt.</p>
  
  <p>Is this the approach being used in industry to prevent enterprise apps from crashes?</p>
  
  <p>If <code>try/catch</code> is really, really our need then is it possible to attach an exception handler with UI thread or other threads and catch everything there? That will be a better approach if possible.</p>
  
  <p>Yes, empty <code>try/catch</code> is bad and even if we print stack trace or log exception to server, wrapping blocks of code in <code>try/catch</code> randomly across all the app doesn't make sense to me e.g. when every function is enclosed in a <code>try/catch</code>.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>As this question has got a lot of attention and some people have misinterpreted the question (perhaps because I haven't phrased it clearly) I am going to rephrase it. </p>
  
  <p>Here is what developers are doing here</p>
  
  <ul>
  <li><p>A function is written and <strong>tested</strong>, it can be a small function which just initializes views or a complex one, after testing it is wrapped around <code>try/catch</code> block. Even for function which will never throw any exception.</p></li>
  <li><p>This practice is used throughout the application. Sometime stack trace is printed and sometime just a <code>debug log</code> with some random error message. This error message differ from developer to developer.</p></li>
  <li><p>With this approach, app does not crash but behavior of the app becomes undetermined. Even sometime it is hard to follow what went wrong.</p></li>
  <li><p>The real question I have been asking was; <strong>Is it the practice being following in the industry for preventing the enterprise applications from crashes?</strong> and I am not asking about <strong>empty try/catch</strong>. Is it like, users love application which do not crash than applications which behave unexpectedly? Because it really boils down to either crash it or present the user with a blank screen or the behaviour user is unaware of.</p></li>
  <li><p>I am posting a few snippets from the real code here</p>
  
  <pre><code>  private void makeRequestForForgetPassword() {
      try {
          HashMap&lt;String, Object&gt; params = new HashMap&lt;&gt;();
  
          String email= CurrentUserData.msisdn;
          params.put("email", "blabla");
          params.put("new_password", password);
  
          NetworkProcess networkProcessForgetStep = new NetworkProcess(
              serviceCallListenerForgotPasswordStep, ForgotPassword.this);
          networkProcessForgetStep.serviceProcessing(params, 
              Constants.API_FORGOT_PASSWORD);
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  
   private void languagePopUpDialog(View view) {
      try {
          PopupWindow popupwindow_obj = popupDisplay();
          popupwindow_obj.showAsDropDown(view, -50, 0);
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  void reloadActivity() {
      try {
          onCreateProcess();
      } catch (Exception e) {
      }
  }
  </code></pre></li>
  </ul>
  
  <blockquote>
    <p>It is <strong>not</strong> duplicate of <a href="https://stackoverflow.com/questions/16561692/android-exception-handling-best-practice">Android exception handling best
    practices</a>, there OP is trying to catch exception for a <strong>different</strong>
    purpose than this question.</p>
  </blockquote>