Try … catch … finally return value

后端 未结 4 1683
心在旅途
心在旅途 2021-02-07 01:24

I found that it\'s a shame that I can\'t return a return value from a such simple construction as try ... catch ... finally

 def foo: String = {
            


        
4条回答
  •  青春惊慌失措
    2021-02-07 02:08

    This method returns a Unit; you have to return something in the catch block; or I'd rather suggest to change the return type to e.g. Option; or use Try class.

    What is obvious - you don't always have a string in this situation. In Java people tend to ignore the realities, but this is Scala, one can do better.

提交回复
热议问题