0

What I want is to sign out the user if an exception/error happens (instead of crashing the app).

I don't want to add try/catch all over my code (all view controllers for example), I want something simples, one try/catch (or whatever else I need) to catch an exception that happened (and was not handled) anywhere in the code.

How can I achieve this?

Thank you.

Rodrigo Ruiz
  • 4,248
  • 6
  • 43
  • 75
  • Note: Do-Try-Catch is for catching *errors* that are thrown. It does not work with *exceptions*. – Eric Aya May 23 '16 at 20:59
  • You can refer http://stackoverflow.com/questions/1282364/how-do-you-implement-global-iphone-exception-handling – mithlesh jha May 23 '16 at 21:08
  • @mithleshjha I tried that, but I still get the crash with "fatal error: unexpectedly found nil while unwrapping an Optional value" – Rodrigo Ruiz May 23 '16 at 22:57

1 Answers1

-1

First, do / try / catch in Swift is just an interesting way to handle errors. It is not exception handling.

Objective-C exceptions are considered programming errors. Since they are programming errors, they are not handled. You don't handle programming errors, you fix them. Except for some very rare situations, you don't catch Objective-C exceptions. You let them crash your program. That's intentional.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • I agree! I just want it to make my life easier while developing, because for my particular case, I might run into some problem (details don't matter right now) and then I have to comment some code, sign out, and sign back in to fix the bad state. – Rodrigo Ruiz May 23 '16 at 22:54
  • 2
    Completely disagree - Exception handling (e.g. such as that from any other language - .NET, Java) has it's place. Not all Exceptions are programming errors, some may be due to state or any other number of inputs. Others can be an exception beyond the control of the program. – csmith Sep 03 '17 at 15:18