What are the differences between throws and rethrows in Swift?

后端 未结 2 776
慢半拍i
慢半拍i 2020-12-04 11:03

After searching for some references to figure it out, -unfortunately- I could not find useful -and simple- description about understanding the differences between thro

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 11:41

    Just to add something along with Martin's answer. A non throwing function with the same signature as a throwing function is considered a sub-type of the throwing function. That is why rethrows can determine which one it is and only require try when the func param also throws, but still accepts the same function signature that doesn't throw. It's a convenient way to only have to use a do try block when the func param throws, but the other code in the function doesn't throw an error.

提交回复
热议问题