0

I'm new to Swift.. I'm trying to do a try catch to make sure I'll not have any problems when I try to remove a row from my tableview and from my notes array.

Here's the code

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    do{
        try removerowtableview(indexPath: indexPath)
    }catch{
            print(error.localizedDescription)
    }
}


func removerowtableview(indexPath: IndexPath) throws{

    dataModel.notas.remove(at: 1000) //1000 to force to crash

    self.tableView.deleteRows(at: [indexPath as IndexPath], with: UITableViewRowAnimation.fade)

    dataModel.saveData()
}

dataModel.notas is my array of notes and I want it to not crash the app and print the error, but I get terminating with uncaught exception of type NSException. Why? What am I doing wrong?

Thanks in advance and sorry for some English mistakes.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
luidgi27
  • 324
  • 2
  • 15
  • 2
    Inside `removerowtableview` there is no function that would throw an error and runtime exceptions are usually non-recoverable, so a do-catch block doesn't help against these. The only way to get rid of most runtime exceptions is to write correct code in the first place. – Dávid Pásztor Aug 07 '17 at 15:26
  • oh ok.. I thought that with that generic `catch` I will get all kind of errors. thanks – luidgi27 Aug 07 '17 at 15:30
  • 2
    You *cannot* catch arbitrary runtime exceptions in Swift, compare e.g. https://stackoverflow.com/questions/38737880/uncaught-error-exception-handling-in-swift. – Martin R Aug 07 '17 at 15:30

0 Answers0