-
-
Notifications
You must be signed in to change notification settings - Fork 602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Error Handling in Swift 2.0 + Update README #220
Comments
Thanks for filing an issue @Dschee. It definitely needs to be more clarification. But Swift's error handling is separate from Objective-C's exception handling. // swift does not catch this exception
do {
NSException(name: "hello", reason: "world", userInfo: nil).raise()
} catch error {
// ...
} While it's good practice to throw exceptions to indicate programmer error, various systems do throw exceptions (eg - CoreData). Also testing throwing of exceptions is useful to test in a library. Nimble also does handle unexpected swift errors. Also there's a |
Thanks for the clarification @jeffh, but I'm not sure I understood all you wanted to say correctly. Does the throwError matcher also match against Swift exceptions? Or is what you want to say that Swift still doesn't have exceptions but instead "throws errors"? In any case, I didn't see the Maybe we should change it to something like:
Or do I still misunderstand? |
Sadly, there's limited vocabulary for these definitions. Yes, Swift doesn't have exceptions, but it has syntactic sugar around errors. Hence the differing names of Exceptions are generally associated to its common implementations:
All of which make exceptions are slower than return values. That's why I'm afraid of using the term In comparison, errors are more accepted as a data structure that is returned by a function. A perfect example is to see how the go programming language handles errors: // go code
func stuff() error {
f, err := os.Open("filename.ext") // Open can fail
if err != nil {
// handle error: 98% time we return the error to our caller
return err
}
// do more stuff
} Obviously this is annoying to write an // swift code
func stuff() throws {
let f = try open("filename.ext")
// do more stuff
} Which is much more concise. When handling errors, swift uses syntax similar to how developers are familiar with exceptions: do {
try stuff()
} catch let error {
// log error
} But that can be a simple conversion to an let error = try stuff() // fake: pretending `try` returns the error
if error != nil {
// log error
} Of course, it's useful to quote a more authoritative source about swift's implementation. A note under the "Handling Errors" section of the Swift Language Guide that says:
This is the reason why Swift always refers them to errors, and not exceptions. All that as a long-winded way to say:
And with the above explanation, you can say:
I hope that was a bit clearer 😉. |
@jeffh Thank you very much for that very detailed explanation. I realize now that I had fallen into the trap to assume I'm dealing with real exception handling when using But in case you care about people who could fall into the same trap as I did, you might want to alter the README to state something like this (providing your great explanation above):
|
Which explains more in detail about the differences between exceptions and errors.
Done, thanks for the feedback @Dschee. 👍 |
Which explains more in detail about the differences between exceptions and errors.
Add BeforeEachTests+ObjC tests
I just read this in the README:
This is not true any more. Swift 2.0 introduced Exceptions and Error handling in general – alongside
guard
anddefer
statements to make this easier. There are three things to clear out as far as I can see:The text was updated successfully, but these errors were encountered: