Java return value (in try/catch clause)

后端 未结 6 362
生来不讨喜
生来不讨喜 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 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.

提交回复
热议问题