java - 扔不工作

标签 java try-catch throw

我有一段代码让我抓狂。 大体的流程是,当TRY中的某个事件发生时,我抛出异常...根据我的理解,每当调用throw时,它只是停止在同一个类中进一步执行并返回从调用此类函数的位置返回控制...

这是代码...

try{
    session = getHibernateSession();
    companyAccountLinkingWSBean = (CompanyAccountLinkingWSBean) wsAttribute
            .getBeanObject();

    companyToMatch = companyAccountLinkingWSBean.getCompanyCode();
    cnicToMatch = companyAccountLinkingWSBean.getCnic();

    LOG.debug("We have found the Mobile number from the WS Bean as input");

    mobile = companyAccountLinkingWSBean.getMobileNumber();
    LOG.info("Mobile is : " + mobile);
    if(mobile.isEmpty()){
        LOG.info("Coming in mobile.isEmpty()");
        companyResponceWSBean = new CompanyResponceWSBean();
        companyResponceWSBean.setpID(Constants.INPUT_MOBILE_ERROR);
        companyResponceWSBean.setMessage(Constants.INPUT_MOBILE_ERROR_MSG);
        companyResponceWSBean.setSuccess(false);
        response = new WSAttribute();
        response.setBeanObject(companyResponceWSBean);
        LOG.info("BEFORE THROWING");

        throw new PayboxFault(Constants.INPUT_MOBILE_ERROR, 
                Constants.INPUT_MOBILE_ERROR_MSG);
    }
    LOG.info("Out side IF statement!!");
} catch (Exception e) {
    LOG.info("IN Exception!!");
}
LOG.info("Out Side Exception . . . Before Returning ");
return response;  

当空移动字段作为输入时在 LOG 中输出 ...

We have found the Mobile number from the WS Bean as input

Mobile is :

Coming in mobile.isEmpty()

BEFORE THROWING

IN Exception!!

Out Side Exception . . . Before Returning

这怎么可能?

最佳答案

你的理解不太正确。捕获异常处理它,并且在catch完成后,流程将在try/catch之后继续,除非你抛出另一个异常,或者重新抛出异常。这可能会更好地解释它:

try {
    // Happy case flow here ...
    throw new PayboxFault(...);
    // If an exception is thrown, the remainder of the `try` block is skipped
} catch (Exception e) {
    // The exception is now handled ...
    //  Unless a new exception is thrown, or the caught exception re-thrown
} finally {
   // Code here should always be called after the try OR catch block executes, 
   // Finally is called even if the catch re-throws
}
// Code after the try-catch-finally executes if the try completes successfully 
// OR if the exception is handled in the catch, but not if the catch re-throws

关于java - 扔不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20739605/

相关文章:

Javascript 抛出事件?

java - 如何使用从表中选择数据,进行计算,然后将新数据更新到与第一个主键相同的另一个表中?

JavaFX如何仅在选中框时显示文本字段

javascript - 在抛出的对象中传递错误原因/如何处理现代 JavaScript 中的错误

javascript - `throw new Error`和 `throw someObject`有什么区别?

c++ - 如何在 cppunit 中断言语句抛出 Excp1 或 Excp2 类型的异常?

java - Spring 数据休息: Different resource returned from received

java - Clojure deftype : how to constrain field types?

.net - 如何在.NET中的catch中重新执行错误的行

null - 在 Kotlin catch block 中设置一个 val