Java return value (in try / catch clause)

all. I have a question about return value in java. Here is my code.

@Override public long addDrugTreatment(long id, String diagnosis, String drug, float dosage) throws PatientNotFoundExn { try { Patient patient = patientDAO.getPatientByDbId(id); long tid = patient.addDrugTreatment(diagnosis, drug, dosage); Connection treatmentConn = treatmentConnFactory.createConnection(); Session session = treatmentConn.createSession(true, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(treatmentTopic); TreatmentDto treatment = null; ObjectMessage message = session.createObjectMessage(); message.setObject(treatment); producer.send(message); return tid; } catch (PatientExn e) { throw new PatientNotFoundExn(e.toString()); } catch (JMSException e) { logger.severe("JMS Error: " + e); } } 

Eclipse reports the error "This method should return a result of type long." However, I returned tid to the try block; eclipse suggests adding a return value after the try / catch block, which breaks the logic. Could you tell me what is wrong here? Thanks.

+6
source share
6 answers

When a JMSException , the return value is undefined. When an exception is thrown, control immediately passes to the exception handler. In this case, you are reporting an error. Then control continues from this point, which goes to the end of the function, without returning a value. You need to either return the value or throw an exception.

+9
source

In Java (or any other C-like language), all control paths must return a value.

If an exception is thrown inside try , then return will not be executed and therefore you will not return a value on all possible control paths.

You must either:

  • add return after try-catch or
  • add return inside each catch or
  • add finally with return .
+3
source
 catch (JMSException e) { logger.severe("JMS Error: " + e); //You need to throw exception here or return something //better would be throw new Exception("JMS Error: " + e); } 
0
source

There is a route through your code, which means that the return value will not be determined, which is an error, since your method says that you will always return a long one.

Do you expect to return a value if the code throws a JMSException? If so, perhaps declare tld out of attempt with a default value.

Otherwise, did you really want to throw a JMSException?

0
source

The problem is that using methods, no matter what route it takes, should return the specified type if the method is completed. So your problem in this code is the second catch. The first catch throws an error, and therefore the method does not execute and therefore does not require a return statement. However, in the second catch, you simply print an error, so the method will be executed to the end, so it should return a long one. The way to solve this problem is to either put it too long for your code to be returned in the second catch, or throw a JMSException and throw it in the code that calls this method. Note that if you throw the catch, you will have to add a throw to the method declaration.

0
source

Suppose a JMSException is thrown:

 @Override public long addDrugTreatment(long id, String diagnosis, String drug, float dosage) throws PatientNotFoundExn { try { Patient patient = patientDAO.getPatientByDbId(id); long tid = patient.addDrugTreatment(diagnosis, drug, dosage); Connection treatmentConn = treatmentConnFactory.createConnection(); //JMS thrown above. No code from here gets executed } catch (PatientExn e) { throw new PatientNotFoundExn(e.toString()); } catch (JMSException e) { logger.severe("JMS Error: " + e); } } 

In the above example, if a JMSException is thrown, no part of the code returns long.

0
source

Source: https://habr.com/ru/post/913044/


All Articles