1

HI i am a little confused about how to transfer if_else error handling to do try catch successfully.

Here is my code.

let error : NSError?
if(managedObjectContext!.save()) {
    NSNotificationCenter.defaultCenter().postNotificationName("updateUndoState", object: nil)    
    if error != nil {
       print(error?.localizedDescription)
    }
}
else {
    print("abort")
    abort()
}

and now i converted to swift 2.0 like this

do {
   try managedObjectContext!.save()
}
catch {
     NSNotificationCenter.defaultCenter().postNotificationName("updateUndoState", object: nil)
     print((error as NSError).localizedDescription)
}

I am confused about where to print abort and do the abort() function

Any idea~? Thanks a lot

J.Chang
  • 35
  • 5
  • In your original code, where does `error` come from? It is not coming from `save()`. – Sulthan Oct 02 '15 at 15:32
  • @Sulthan thanks for ur remind. However after changing my code, it occurs that : Variable 'error' used before being initialized – J.Chang Oct 02 '15 at 15:42
  • @Sulthan - No, the `error` _is_ thrown by the `save()`. When you have `catch` without a `let`, it automatically uses `error` as a reference to the thrown error. – Rob Oct 02 '15 at 15:44
  • @Rob I was speaking about the `error` in the first example. – Sulthan Oct 02 '15 at 15:46
  • Ok. That first example was obviously a typo because it isn't valid Swift 1.2 code. So I assumed you must have meant the second code snippet. But you're right that that first code snippet is missing the error reference in the `save()` call. – Rob Oct 02 '15 at 15:50
  • The original Swift 1 code wouldn't work in the first place. The error would never be logged. That's why I assumed the `error` variable contains a different error. – Sulthan Oct 02 '15 at 15:52
  • Lol. Yeah, because Swift 1.2 code is missing a required `error` parameter to `save`, I assumed that was the root of the typo. Regardless, the first code snippet is (still) not correct. – Rob Oct 02 '15 at 15:58

2 Answers2

1

Rewriting your code to work the same as your original code

do {
   try managedObjectContext!.save()

   //this happens when save did pass
   NSNotificationCenter.defaultCenter().postNotificationName("updateUndoState", object: nil)    

   //this error variable has nothing to do with save in your original code
   if error != nil {
       print(error?.localizedDescription)
   }
}
catch {
   //this happens when save() doesn't pass
   abort()
}

what you probably want to write is the following:

do {
   try managedObjectContext!.save()

   //this happens when save did pass
   NSNotificationCenter.defaultCenter().postNotificationName("updateUndoState", object: nil)    
}
catch let saveError as NSError {
   //this happens when save() doesn't pass
   print(saveError.localizedDescription)
   abort()
}
Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • I now understand your first code snippet. You assumed that he had some other `error` variable floating around. His revised question makes it clear that this is not the case, but I now get why you wrote what you wrote. – Rob Oct 02 '15 at 16:03
1

Everything within do {} is good, everything within catch {} is bad

do {
   try managedObjectContext!.save()
   NSNotificationCenter.defaultCenter().postNotificationName("updateUndoState", object: nil)
}
catch let error as NSError {
     print(error.localizedDescription)
     abort()
}

use either the error handling or the abort() statement

vadian
  • 274,689
  • 30
  • 353
  • 361