简体   繁体   中英

Java return value (in try/catch clause)

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.

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.

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

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM