0

Can any one tell me what is the best to handle exception in iOS (Objective C)?

I have the following two approaches:

  1. Use try/catch block.
  2. Do proper data handing for all model class.

What do you suggest?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Rahul Juyal
  • 2,124
  • 1
  • 16
  • 33

2 Answers2

2

Exceptions in Objective are only meant to be used for programming errors where there is no recovery (the app will terminate immediately).

Exception are not designed to be used for program control flow in Objective-C. Check for all possible errors and handle them at that point.

Further, there is no cleanup on catches across stack frames so full recovery is generally not possible.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • "fun recovery"? Did you mean "full recovery"? I like the idea of "fun recovery" though. :) – rmaddy Feb 11 '16 at 17:05
  • Oops, yes full, fixed, thanks! @rmaddy you should know by now that I am a bad speller, worst typer and combining that with auto-correct is a recipe for disaster. ;-) – zaph Feb 11 '16 at 17:06
0

On iOS, you should fix whatever is causing the exception. NSError is for errors that are expected (e.g. Backend returns invalid or not data). Exceptions are usy programming errors and should be fixed. Avoid try/catch, except when it is explicitly stated you should use it (e.g. Subclassing NSOperation).

Joride
  • 3,722
  • 18
  • 22