我有一段代码让我抓狂。
大体的流程是,当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/