Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 979
Maciej Jastrzebski Avatar asked Jun 09 '15 19:06

Maciej Jastrzebski


People also ask

What is the difference between Swift and Java programming language?

08. Java has complex syntax and code readability as compared to swift language. Swift has easy syntax and code readability as compared to java language 09. Some of the companies which use Java programming language are Uber, Google, Netflix, Pinterest, Instagram and Amazon etc.

What is the difference between object creation in Java and Swift?

In java, object creation looks like Simple obj=new Simple (); 07. Java is one of the old programming language having more community support. Swift is one of the new programming language having less community support as compared to java. 08. Java has complex syntax and code readability as compared to swift language.

How to handle errors in Swift?

In Swift 2.0, Apple introduced a new way to handle errors (do-try-catch). And few days ago in Beta 6 an even newer keyword was introduced (try?).

How can you tell the difference between a swallow and a swift?

One way to tell the difference between a swift and a swallow when looking up at the sky is its silhouette. Swifts have scythe-shaped wings, and a slightly blunt forked tail, while swallows have a much longer, trailing forked tail and more rounded wings.


1 Answers

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")

like image 70
vrwim Avatar answered Nov 04 '22 10:11

vrwim