0

Is there in objective-C any way to see if it is possible to do a certain task without risking crash the application?

Other languages like javascript have

try
  {
  //Run some code here
  }
catch(err)
  {
  //Handle errors here
  }

is there something in Objective-C? If so, what is the syntax?

thanks.

Duck
  • 34,902
  • 47
  • 248
  • 470

3 Answers3

17

While Felix's answer is technically correct, it isn't the whole story.

On the iOS and Mac OS X, exceptions are used to indicate non-recoverable program errors. Exceptions are not used to indicate user error or recoverable errors like on Java.

Any exception that passes through system framework code will leave said framework in an undefined state.. Catching said exceptions and trying to recover from it will lead to memory leaks, undefined behaviour and crashing.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
bbum
  • 162,346
  • 23
  • 271
  • 359
  • so, there's no hope of existing anything that can be used like it was in Javascript and other languages? – Duck Jul 31 '10 at 18:47
  • Nope. The design pattern used is that exceptions are uncoverable errors. – bbum Aug 01 '10 at 16:59
  • If exceptions are unrecoverable errors, why does the syntax include a way to catch and recover from them, or to execute code even when an exception is thrown, instead of simply calling out to the installed NSExceptionHandler and forcing termination? – Seth Kingsley Oct 17 '11 at 21:14
  • 4
    Just because the language has syntax to support something, doesn't mean the frameworks are designed to support that thing, too. The frameworks have a very firm policy: exceptions are unrecoverable errors. Trying to catch and recover goes against framework design and, quite clearly, an exception that "jumps over" framework based stack frames is a violation of documented policy and will yield unspecified behavior. – bbum Oct 17 '11 at 21:17
4

Have a look at Exception Handling :

Cup *cup = [[Cup alloc] init];

@try {
    [cup fill];
}
@catch (NSException *exception) {
    NSLog(@"main: Caught %@: %@", [exception name], [exception reason]);
}
@finally {
    [cup release];
}

Also worth to have a look: Error Handling Programming Guide

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 1
    Don't do that.exceptions are hard errors on iOS and are not mesntto be recovered from. – bbum Jul 31 '10 at 17:58
  • @bbum: Not custom exceptions you might throw. But I agree, for general error handling, this documentation might be better: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ErrorHandlingCocoa/ErrorHandling/ErrorHandling.html#//apple_ref/doc/uid/TP40001806 – Felix Kling Jul 31 '10 at 18:03
  • 1
    Even custom exceptions. If those exceptions go through any frame of system code, the behaviour is undefined. Exceptions for flow control are only safe in 100% isolation from framework code. – bbum Jul 31 '10 at 18:08
  • @bbum, this is too old, but I read your comment (the first one), there's a word "mesntto"... what doest it mean? I searched in google and there's no result for this. Please tell. – Hemang Mar 21 '15 at 05:44
  • @hagile: it probably is supposed to mean "meant to". – Felix Kling Mar 21 '15 at 05:45
  • @FelixKling, Thanks! I was really wonder of this word which I heard for the first time. :) – Hemang Mar 21 '15 at 05:48
0

The real question is, what are you trying to do in that error catching block?

There are different ways to hook into different kinds of errors. What kind of code specifically are you looking to wrap in a block like that?

Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150