繁体   English   中英

减少Java代码中的try-catch块以提高性能

[英]Reducing try-catch blocks in Java code to improve performance

我的任务是减少Java代码中的try-catch块以提高性能。 但是每个try块都在检查一种完全不同的异常以及自定义异常。 如何减少try-catch块。

我的代码的一部分示例如下:-

        // 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;
        }

如果在这种情况下每个块的异常类型都不同,则可以将try块加入一个块,然后将多个catch块添加到一个try块中

try {
    TestEngine testEngine = objFactory.getTestEngine(login.getTestengine());

    //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 = testEngine.buildTestTaker(login, null, sessionId, null, null);
} 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;
} 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;
}

您可以将两个try-catch块更改为一个:

TestEngine testEngine = null;
Testtaker testTaker = null;

try {
  testEngine = objFactory.getTestEngine(login.getTestengine()); 

  String saveSessionId = 
    objFactory.getConfigValue("testengine." + login.getTestengine() + ".recordjessionid", "false");

  String sessionId = saveSessionId.trim().equals("true") ? 
    request.getSession().getId() : null;

  testTaker = testEngine.buildTestTaker(login, null, sessionId, null, null);
}
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;
}
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 e;
}

我会这样写

 public void someMethod(... args) throws Exception {
    // taker
    TestEngine testEngine = objFactory.getTestEngine(login.getTestengine());

    // 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 =  testEngine.buildTestTaker(login, null, sessionId, null, 
}

而且我会让调用者处理任何异常。

与C ++不同,try-catch-finally块(异常)是Java的基本组成部分。 它们应该被正确使用和使用。 我也不认为它们对性能有重大影响; 即使您没有抓住它们,它们也将被抛出(最终将被主线程抓住)。 但是出于美学原因,您可以对其进行重新组织,或者将单个方法用于整个方法,如下所示:

method() {
 try {
 } catch (Ex1 e1) {
 } catch (Ex2 e2) {
 } finally {
 }
}

您还可以考虑处理它们,而不是再次抛出该异常,并在调用堆栈中的每个方法中进行处理(这可能会对性能产生一点影响。)

正如评论所指出的那样,try / catch块的数目不是问题,这是事实,您是如此频繁地击中它们。

假设您已经分析了性能,而实际上正是这种方法给您带来了问题,那么您应该采取显而易见的步骤来避免引发异常(结果是堆栈被解开)。

例如,如果在调用getTestEngine()之后testEngine为null,则不会从该方法返回,但是您将在testEngine.buildTestTaker()之后立即获得NPE,这将导致您的catch块之一被击中。 相反,如果testEngine为null,则应从方法中返回(带有适当的错误代码),以避免堆栈testEngine

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM