After i update swift 2.0 i got an error with do { try } catch like an image below.
How can i fix this? Thanks!
The try/catch syntax was added in Swift 2.0 to make exception handling clearer and safer. It's made up of three parts: do starts a block of code that might fail, catch is where execution gets transferred if any errors occur, and any function calls that might fail need to be called using try .
You use a do - catch statement to handle errors by running a block of code. If an error is thrown by the code in the do clause, it's matched against the catch clauses to determine which one of them can handle the error.
Where is the Reachability type defined? Maybe it has a throw -ing default initializer? The issue is that the initializer you are using is marked with 'throws' which means that it can throw an exception when it encounters a problem.
Swift is much stricter with respect to error handling and that is a good thing. The syntax, for example, is much more expressive. The try keyword is used to indicate that a method can throw an error. To catch and handle an error, the throwing method call needs to be wrapped in a do-catch statement.
The error is telling you that the enclosing catch is not exhaustive. This is because the auto-generated catch
block is only catching NSError
objects, and the compiler can't tell whether some other ErrorType
will be thrown.
If you're sure no other errors will be thrown, you can add another default catch block:
do { objects = try managedObjectContext?.executeFetchRequest(request) } catch let error1 as NSError { error = error1 objects = nil } catch { // Catch any other errors }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With