4

I have try-catch blocks when removing KVO observers, like this

@try {

    [table_ removeObserver:self forKeyPath:@"contentOffset" context:nil];

} @catch (NSException __unused *exception) {}

Also, I have the "All exceptions" breakpoint set, but I don't want it to stop when the exception is __unused, like the previous one.

Is that possible? I read some answers here but it is different, I think.

Thank you.

Community
  • 1
  • 1
emenegro
  • 6,901
  • 10
  • 45
  • 68
  • 1
    I'm pretty sure the all exceptions breakpoint occurs when an exception is raised, not caught. Maybe you can change the setting to on catch? It still might not work... – Brad Allred Jan 31 '14 at 15:04
  • What are you actually trying to achieve here? Why do you think you might get an exception? If removeObserver throws an exception, then it is due to a programming error, and you fix the error instead of hiding it by catching the exception. This is Objective-C, not Java or C++. – gnasher729 Apr 14 '14 at 13:32
  • You can't know if an object is observer of a KVO property so instead of maintainig a prone-error structure to track which object is observer of a certain object's property you use an unused exception. It's a very common convention in Obj-C and you avoid complex flags, dictionaries or whatever you want to use to achieve this. Do you use tjHe same approach to deregister NSNotificationCenter observers? No, because there is no exception thrown. This is simply clean code, less code less bugs. – emenegro Apr 14 '14 at 17:28

0 Answers0