How to return a value from try, catch, and finally?

前端 未结 4 922
情歌与酒
情歌与酒 2020-12-18 18:30

So when I do a code of blocks inside a try{}, and I try to return a value, it tells me

no return values

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-18 19:22

    It is because you are in a try statement. Since there could be an error, sum might not get initialized, so put your return statement in the finally block, that way it will for sure be returned.

    Make sure that you initialize sum outside the try/catch/finally so that it is in scope.

提交回复
热议问题