'Throw NOT working
I have a piece of code that has driven me nuts.
The general flow is that when a certain event in TRY occurs, I throw the exception... according to my understanding, whenever the throw is called, it simply stops further execution in the same class and return the control back from where this class's function was called...
Here is the code...
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;
Output in LOG When Empty mobile field is given as input ...
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
How is it actually possible?
Solution 1:[1]
Your understanding isn't quite correct. Catching an exception handles it, and after the catch is completed, flow will continue after the try/catch, unless you throw another exception, or re-throw the exception. This might explain it better:
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
Solution 2:[2]
When you catch an exception, execution continues from after the catch block. If you don't want it to, you'll need to return from inside your catch (or not catch the exception at all and let it bubble up to the caller).
catch (Exception e)
{
LOG.info("IN Exception!!");
return null;
}
Or:
catch (Exception e)
{
...
throw e; // rethrow exception.
}
Note that if you do this, the exception will continue to bubble up the call stack until you catch it somewhere else (such as in the calling method).
Solution 3:[3]
You are catching the Exception. So it will not transfer back the control to the calling function unless entire function is executed.
If you want to skip the
LOG.info("Out Side Exception . . . Before Returning ");
statement you can do
catch (Exception e) {
LOG.info("IN Exception!!");
throw e;
}
And about
when ever the throw is called, it simply stops further execution in the same
class and return the control back from where this class's function was called...
Yes only if you do not catch it. You catch an Exception if you intent to do something with it which includes logging it and rethrowing.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Aniket Thakur |
