Exception handling in iphone?

What is the syntax for handling exceptions in iphone sdk? how to handle exceptions in iphone. What documentation do you need to know more? textbook, sample code are most in demand and grateful.

+4
source share
2 answers

Exceptions in Objective-C are a pretty controversial issue, even Apple itself will prevent you from using them unless absolutely necessary.

My first question would be what do you want to achieve from exception handling? If you look from the point of view of Java and how much exceptions are so tightly integrated into this language for error handling (for example, flow control), I think that it is not practical to use Objective-C exceptions for this purpose, you need to use NSError errors and handle errors this way .

This is a fragment of a documentation document: -

Exceptions are resource intensive in Objective-C. You should not use exceptions for general flow control, or simply mean errors. Instead, you should use the return value of a method or function to indicate that an error has occurred, and information about the problem in the error object. For more information, see Cocoa Bug Programming Guide.

+6
source

See Apple's documentation on exception handling in Objective-C:

The main example from the docs:

Cup *cup = [[Cup alloc] init]; @try { [cup fill]; } @catch (NSException *exception) { NSLog(@"main: Caught %@: %@", [exception name], [exception reason]); } @finally { [cup release]; } 
+4
source

Source: https://habr.com/ru/post/1308164/


All Articles