Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errors thrown from here are not handled for do { } catch in Swift 2.0

Tags:

xcode

ios

swift

After i update swift 2.0 i got an error with do { try } catch like an image below.

enter image description here

How can i fix this? Thanks!

like image 747
Varis Darasirikul Avatar asked Sep 18 '15 10:09

Varis Darasirikul


People also ask

Do try catch block in Swift?

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 .

Do catch statement in Swift?

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.

Can Call throw but is not marked with try?

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.

What is the difference between try and try in Swift?

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.


1 Answers

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  } 
like image 180
Ronald Martin Avatar answered Oct 13 '22 12:10

Ronald Martin