What is the difference between Swift 2.0 do-try-catch and regular Java/C#/C++ exceptions

后端 未结 1 1495
滥情空心
滥情空心 2020-12-15 11:25

It seems that Swift 2.0 has changed from traditional ObjC (NSError returning) and Swift 1.X (Success/Failure optionals) conventions of runtime error handling, to something t

相关标签:
1条回答
  • 2020-12-15 11:59

    There are 3 major differences I have found:

    1. It is not necessary to list all errors a function can throw, only a throws keyword is needed.

    2. There is no significant slowdown when using these errors, while Java and other languages need to construct an Exception object and unwind the stack. In Swift a throws keyword can be viewed as the function returning an Either-object, with one being the original return type, and the other being an ErrorType value.

    3. In Swift all errors need to be handled or declared to be thrown, it is impossible to get an error from a method that does not state it is throwing an error. (in Java terms, all errors are "checked exceptions")

    0 讨论(0)
提交回复
热议问题