将多个try-catch块组合成一个

时间:2014-05-19 13:37:30

标签: java exception-handling try-catch code-cleanup

以下是代码示例块

// rebuild session state for test, need the current phase first
String phaseName = testtaker.getPhase();
TestPhase phase = null;

try {
    phase = objFactory.getPhase(testtaker.getTestengine(), phaseName);
} catch (Exception e) {
    log.error("Exception when getting phase for testtaker.", e);
    CaslsUtils.outputLoggingData(log_, request);
    // Toss the exception back to the engine, add a message
    throw new Exception("Cannot create phase: " + phaseName);
}

try {
    // Adding this code to improve the performance of the application during student login
    Testtaker newTestTaker = objFactory.getDataAccessor().getTesttakerByTestTakerId(testtaker.getTesttakerid());// This will populate testtaker object with all its child table records
    if(newTestTaker != null) {
        session.setAttribute(ConstantLibrary.SESSION_TESTTAKER, newTestTaker);
    }
    phase.rebuildTest(session);
} catch (Exception e) {
    log.error("Exception when calling rebuildTest.", e);
    CaslsUtils.outputLoggingData(log_, request);
    // Toss the exception back to the engine, add a message
    throw new Exception("Cannot Rebuild Test");
}

请帮助我如何在多捕获语句中组合此代码,因为当我尝试这样做时,它会在第二个catch块中生成错误作为无法访问的代码。

0 个答案:

没有答案