1

I would like to subclass NSException and use it in my app for the application specific errors or exceptions.

Is it right way to do so? If YES, how to do it in a better way. If NO, why it is not the right way and whats the alternate for it?.

Please guide me !

Thanks.

Adam
  • 26,549
  • 8
  • 62
  • 79
Easwaramoorthy Kanagaraj
  • 3,925
  • 8
  • 36
  • 62
  • See [this question](http://stackoverflow.com/q/14615350/730701). – Adam May 20 '13 at 14:12
  • Enlighten yourself: http://stackoverflow.com/a/14615438/667586 – Abhishek Bedi May 20 '13 at 14:16
  • Subclassing seems like a legit way! Came across a nicely written article on [exception handling in ObjC](http://club15cc.com/code/objective-c/dispelling-nsexception-myths-in-ios-can-we-use-try-catch-finally). Subclassing of NSException class is mentioned towards the end. – Amar May 20 '13 at 14:20
  • 1
    Thing is: Objective-C isn't Java. You can't just subclass willy-nilly and expect complicated mechanisms like stack unwinding and exception handling to work properly. NSException has roots deep in the system, it's not just an abstract class, and even still, exceptions are *not* a valid means of control flow (unlike other languages). Don't subclass NSException, `+raise:` one. – CodaFi May 20 '13 at 14:26
  • @CodaFi Thanks for your comment. Could you please guide me with some more explanation reasoning why it should not and post your answer please. – Easwaramoorthy Kanagaraj May 20 '13 at 14:39
  • 1
    I've updated my other answer, and I'm voting to close this as a duplicate. Hopefully that helps you instead of me posting nearly the same answer to two questions. – CodaFi May 20 '13 at 14:40

1 Answers1

3

Do not use exceptions for recoverable errors.

So, yes, there is a better way. Exceptions should only be used for fatal errors. Other kinds of error handling is done via the NSError** pattern.

CodaFi's answer (Is it a good practice to subclass from NSException for app-specific exceptions?) has more details.

Community
  • 1
  • 1
bbum
  • 162,346
  • 23
  • 271
  • 359