Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'no calls to throwing functions occur within 'try' expression [warning] in swift 3

I'm trying to delete an object from core data and I tried it within try catch as below.

do {
   try self.managedContext.deleteObject(self.productList[indexPath.row])
} catch let error as NSError {
        print("something wrong with the delete : \(error.userInfo)")
}

it say 'no calls to throwing functions occur within 'try' expression and 'catch block is unreachable because no errors are throw in 'do' block. following image give you more idea.

enter image description here

why is this. I have no idea. how to solve this. hope your help.

like image 875
Chanaka Anuradh Caldera Avatar asked Dec 23 '22 23:12

Chanaka Anuradh Caldera


2 Answers

The deleteObject method doesn't throw. Remove the Do-Catch block and the warning will go away. The warning seems pretty self explanatory.

like image 166
TheValyreanGroup Avatar answered Apr 19 '23 15:04

TheValyreanGroup


Make sure that the method that you are invoking after try throws.

Example:

func someThrowingFunction() throws -> Int {
    // ...
}

let x = try? someThrowingFunction()

let y: Int?
do {
    y = try someThrowingFunction()
} catch {
    y = nil
}

more information about that here: ErrorHandling

like image 30
Pedro Trujillo Avatar answered Apr 19 '23 13:04

Pedro Trujillo