What is the difference between Swift 2.0 do-try-catch and regular Java/C#/C++ exceptions
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 that looks very similar to exception handling in languages like Java/C#/C++/etc.
Apple has traditionally emphasized use of
NSError instead of throwing
NSException for runtime errors (vs programmer errors), as
NSException stack unwinding could cause memory leaks with default ObjC compiler settings.
Now they have however devised something that looks very, very similar to traditional exceptions. My question is:
Are there any real differences between Swift 2.0 error handling and traditional exception handling beside nomenclature (error vs exception) and syntax (do-catch, instead of try-catch, try used before method call, etc).
- Generic function taking a type name in Swift
- Native Swift implementation of DEFLATE (unzip) algorithm
- Find index of Nth instance of substring in string in Swift
- How to detect all touches in Swift 2
- Core Data NSTimeInterval using an accessor directly is buggy
Solutions Collect From Internet About “What is the difference between Swift 2.0 do-try-catch and regular Java/C#/C++ exceptions”
There are 3 major differences I have found:
It is not necessary to list all errors a function can throw, only a
throwskeyword is needed.
There is no significant slowdown when using these errors, while Java and other languages need to construct an
Exceptionobject and unwind the stack. In Swift a
throwskeyword can be viewed as the function returning an
Either-object, with one being the original return type, and the other being an
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”)
- How to change UIWebView default font
- Swift function that takes in array giving error: '@lvalue $T24' is not identical to 'CGFloat'
- Swift-Upload multiple images to server
- sqlite database update when app version changes on Appstore in iPhone
- iPhone simulator fails
- Make UISlider height larger?
- Force landscape mode in one ViewController using Swift
- iOS 6.x open command line on jailbreak
- Using allegro with Xcode 4
- Red banner comes and disappear suddenly when press home button during recording on going
- cocos2d v3 re-orient screen during App use
- Opening tel: links from UIWebView
- Moving Camera in Google Maps
- How can I mark “To Do” comments in Xcode?
- Getting human readable relative times and dates from a unix timestamp?