Java return value (in try/catch clause)

后端 未结 6 361
生来不讨喜
生来不讨喜 2021-01-12 17:25

everyone. I have a rookie question about the returning value in java. Here\'s my code.

@Override
public long addDrugTreatment(long id, String diagnosis, Stri         


        
相关标签:
6条回答
  • 2021-01-12 17:56
    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 讨论(0)
  • 2021-01-12 18:01

    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.

    0 讨论(0)
  • 2021-01-12 18:04

    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.

    0 讨论(0)
  • 2021-01-12 18:05

    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.

    0 讨论(0)
  • 2021-01-12 18:07

    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?

    0 讨论(0)
  • 2021-01-12 18:16

    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.
    0 讨论(0)
提交回复
热议问题