title: Reducing try-catch blocks in Java code to improve performance
body: <p>I have been given the task of reducing try-catch blocks in my Java code for increasing performance. But each try block is checking for a entirely different kind of exception and that too custom exceptions. How to reduce the try-catch blocks.</p>
  
  <p>The sample of a part of my code is as follows:-</p>
  
  <pre><code>        // Get a test engine and use that to initialize and save the test
          // taker
          TestEngine testEngine = null;
          try {
              testEngine = objFactory.getTestEngine(login.getTestengine());
          } catch (NoTestEngineException e) {
              // Add an error message, then throw the exception to struts to
              // handle
              request.setAttribute("errmsg", "Cannot create test engine: " + login.getTestengine());
              request.setAttribute("errcause", "exception.notestengine.cause");
  
              throw e;
          }
  
          //added for null check of variable testEngine
                  if(testEngine==null)
                  {
                      request.setAttribute("errmsg", "Could not obtain a testengine");
                  }
  
          // Do we need to save the session id?
          String saveSessionId = objFactory.getConfigValue("testengine." + login.getTestengine() + ".recordjessionid", "false");
          String sessionId = null;
          if (saveSessionId.trim().equals("true")) {
              sessionId = request.getSession().getId();
          }
  
  
  
          Testtaker testTaker = null;
          try {
              testTaker = testEngine.buildTestTaker(login, null, sessionId, null, null);
          } catch (Exception e) {
              request.getSession().removeAttribute(ConstantLibrary.SESSION_LOGIN);
  
              CaslsUtils.outputLoggingData(log_, request);
  
              // Add an error message, then throw the exception to struts to
              // handle
              request.setAttribute("errmsg", "Cannot build a test taker.");
              request.setAttribute("errcause", "exception.testtakerbuildfailed.cause");
  
              //throw new NoTestTakerException("Failed to build testtaker.");
              throw e;
          }
  </code></pre>