2

I am trying to catch some exception that I don't know it will be throw in my code. Is there any solution for this?

Shripada
  • 6,296
  • 1
  • 30
  • 30
Ringo
  • 3,795
  • 3
  • 22
  • 37
  • 1
    do{ code that can potentially throw} catch {/*Any exception is caught here*/} – Shripada Dec 10 '15 at 04:41
  • 1
    Is your question about try/throw/catch (which are *errors*, not *exceptions*)? Or about NSExceptions? Or about runtime exceptions (like "index out of range" or "unexpectedly found nil")? – Martin R Dec 10 '15 at 04:45
  • @MartinR It's about all exception as you say. Do I need to handle all of it by myself. Is there any easier way like .net c#? Thanks! – Ringo Dec 10 '15 at 04:48
  • 1
    These are all different. You need a do/try/catch for a throwing function. NSException can be caught globally (http://stackoverflow.com/questions/25441302/how-should-i-use-nssetuncaughtexceptionhandler-in-swift), and runtime exceptions *cannot* be caught as far as I know. – Martin R Dec 10 '15 at 04:53
  • @MartinR Thanks for your help! But how about external library/framework we can not handle their exception also, right? Oh no – Ringo Dec 10 '15 at 04:56
  • Does this answer your question? [Catching NSException in Swift](https://stackoverflow.com/questions/32758811/catching-nsexception-in-swift) – Sindre Sorhus Mar 09 '20 at 14:44

0 Answers0