Missing return statement, what i am missing
Missing return statement, what i am missing
I written a method which will acknowledge the controller by returning true and false, I return true inside try if everything goes fine it will return true and I return false inside catch blocks, but still method shows me error "missing return statement"
what is the best way to do it.
"missing return statement"
The below method written in java will send back the true or false to the controller.
Secondly I want to carry the exception message from here to controller, I think of returning string, is it good approach,
Kindly suggest me the best way to do the exception handling
public boolean pickSalayData(String yearMonth, String regionId, String circleId, Userdetail loginUser) throws MyExceptionHandler
String tableSuffix = yearMonth.substring(4, 6) + yearMonth.substring(0, 4);
log.info("Pick Salary Data From ERP " + DateUtility.dateToStringDDMMMYYYY(new Date()));
List<SalaryDetailReport> detailReports = hRMSPickSalaryDataDAO.findAll(yearMonth, regionId, circleId);
TransactionDefinition def = new DefaultTransactionDefinition();
TransactionStatus trstatus = transactionManager.getTransaction(def);
try
List<SalaryDetailReport> salaryDetailReport = null;
int countDetail = 0;
if (detailReports != null && detailReports.size() > 0)
for (SalaryDetailReport salary : detailReports)
try
if (countDetail % COMMIT_COUNT == 0)
if (salaryDetailReport != null)
salaryDetailReportDAO.save(salaryDetailReport, tableSuffix);
reportHistoryDAO.save(salaryDetailReport, loginUser);
salaryDetailReport = new ArrayList<SalaryDetailReport>();
salaryDetailReport.add(salary);
countDetail++;
catch (Exception e)
log.error("Error on Save Salary Pay Head Details Data from ERP to Prayas .");
if (salaryDetailReport != null && salaryDetailReport.size() > 0)
salaryDetailReportDAO.save(salaryDetailReport, tableSuffix);
reportHistoryDAO.save(salaryDetailReport, loginUser);
else
throw new MyExceptionHandler("No record for Save in Database from ERP.");
salaryDetailReportDAO.update(tableSuffix, regionId, circleId);
List<SalaryDetailReport> reports = salaryDetailReportDAO.findAll(tableSuffix, regionId, circleId);
if (reports != null && reports.size() > 0)
for (SalaryDetailReport salaryDetail : reports)
try
SalaryDetail sd = new SalaryDetail();
sd.setDetailReport(salaryDetail);
salaryDetailDAO.save(sd, tableSuffix);
catch (Exception e)
log.error("Error occured", e);
e.printStackTrace();
throw new MyExceptionHandler(" Error :" + e.getMessage());
System.out.println("data found");
else
log.error("Salary Record Not Found.");
throw new MyExceptionHandler("No record Found.");
salaryDetailDAO.updateEarningDeduction(tableSuffix);
//salaryDetailDAO.updateEarningDeductionsInSDT();
transactionManager.commit(trstatus);
try
hRMSPickSalaryDataDAO.update(regionId, circleId, yearMonth);
return true;
catch (Exception ex)
log.error("Some error : ", ex);
// // System.out.println("Completed =============================");
catch (MyExceptionHandler ex)
transactionManager.rollback(trstatus);
ex.printStackTrace();
log.error("Failed to Save Salary data :" + ex.getMessage());
return false;
catch (Exception ex)
transactionManager.rollback(trstatus);
ex.printStackTrace();
log.error("Error occured on Save Salary data.", ex);
return false;
return
You as a programmer may think that you have all bases covered, but the compiler can not make the same assumptions as yourself, so every
path
needs too have a return at the end– Scary Wombat
Sep 14 '18 at 5:46
path
BTW, remove
e.printStacktrace
and System.out.println
if you use a logging Framework– Jens
Sep 14 '18 at 5:46
e.printStacktrace
System.out.println
But that's the question why, When my try is already cover the entire code, each time try execute or the catch will executed , so i handle the return there, then why i need to return at last
– user9634982
Sep 14 '18 at 5:46
2 Answers
2
You are missing return
statement for the following catch
block :
return
catch
catch (Exception ex)
log.error("Some error : ", ex);
Either you add return
statement in this catch
block or at the end of mehtod
return
catch
Thank you so much, I also need to track which catch block catches the exception, how can I implement that?
– user9634982
Sep 14 '18 at 5:54
Means? Not getting you..
– codeLover
Sep 14 '18 at 6:04
you saw multiple catch blocks inside the method, say catch 1, catch 2, catch 3, I need to customise the error message, in order to do so, I need to figure it out which catch block catches the exception
– user9634982
Sep 14 '18 at 6:08
That entirely depends on your code flow ..moreover you need to study exception navigation
– codeLover
Sep 14 '18 at 6:10
I think to return string concatenated with exception message
– user9634982
Sep 14 '18 at 6:13
If this code throws an Exception, then the following catch
code will not be entered into and hence there is no return value
catch
try
hRMSPickSalaryDataDAO.update(regionId, circleId, yearMonth);
return true;
catch (Exception ex)
log.error("Some error : ", ex);
**edit**
return `true
} catch (...)
return something;
How do I fix it then?
– user9634982
Sep 14 '18 at 5:56
either return
true
or false
based upon your logic, although throwing exceptions to control flow is not a good idea– Scary Wombat
Sep 14 '18 at 5:57
true
false
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You miss a
return
statement at the end of your method– Jens
Sep 14 '18 at 5:44