Java return value (in try/catch clause)

陌路散爱 提交于 2019-12-19 06:45:14

问题


everyone. I have a rookie question about the returning value in java. Here's 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 a "This method must return a result of type long" error. Yet I did return the tid in the try block; eclipse suggests to add a return value after the try/catch block, which would break the logic. Could you please tell me what wrong here? Thanks.


回答1:


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




回答2:


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

If an exception is thrown inside the try then the return will not be executed and so you are not returning a value on all possible control paths.

You have to either:

  1. add a return after the try-catch or
  2. add a return inside each catch or
  3. add a finally with a return.



回答3:


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);
    }



回答4:


There's a route through your code which means there would be no return value defined, which is an error, since your method says you'll always return a long.

Are you expecting a value to be returned when if the code throws a JMSException? If so, perhaps declare tld outside the try with a default value.

Else did you really mean to re-throw the JMSException?




回答5:


The problem here is that with methods no matter which route the code takes it must return the type specified if execution of the method completes. So your problem in this code is the second catch. The first catch throws an error and therefore the method does not complete execution and thus does not need a return statement. However in your second catch you just print an error so the method will execute till the end so must therefore return a long. The way to solve this is to either put an appropriate long for your code to be returned in the second catch or to throw the JMSException and deal with that in the code that calls this method. Note if you throw in the catch you will have to add the throw to your method declaration.




回答6:


Let's imagine a JMSException was 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 if a JMSException is thrown no part of the code returns a long.



来源:https://stackoverflow.com/questions/10125941/java-return-value-in-try-catch-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!