How to enable exception handling in objective-c and xcode

EDIT: problem resolved (partially): this is a simulator error. I built and tested this on two devices with iOS 3.1.3 and 4.0. The exception was handled correctly. Be careful, the simulator is your enemy!

It drives me crazy. I do not know how to enable exception handling in my project. Look at the code and debugger output below.

My goal is to catch the exception without correcting the code so that the exception is handled and the application does not crash.

I am using Xcode 3.2.3, iPhone SDK 4 final. I just created a simple iPhone application for the iPhone to test this.

I looked in the settings of my project, and yes checked the switch "Enable Objective-C Exceptions". I am using GCC 4.2.

When I look at the build process in detail, the compiler flag -fno-objc-exceptions is not on the argument list!

What am I missing here?

Thanks in advance Nick

NSArray * foo = [[NSArray alloc] init];

@try {
   NSLog(@"trying...");  
   [foo objectForKey:@"yeah"];
}
@catch (NSException * e) {
   NSLog(@"catching %@ reason %@", [e name], [e reason]);
}
@finally {
   NSLog(@"finally");
}

leads to

trying...

-[__NSArrayI objectForKey:]: unrecognized selector sent to instance 0x5d5f780

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI objectForKey:]: unrecognized selector sent to instance 0x5d5f780'

*** Call stack at first throw:
(
 0   CoreFoundation                      0x02393919 __exceptionPreprocess + 185
 1   libobjc.A.dylib                     0x024e15de objc_exception_throw + 47
 2   CoreFoundation                      0x0239542b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
 3   CoreFoundation                      0x02305116 ___forwarding___ + 966
 4   CoreFoundation                      0x02304cd2 _CF_forwarding_prep_0 + 50
...
)
terminate called after throwing an instance of 'NSException'

Whether capture or final block is reached.

+3
source share
4 answers

Quote from How to get global exceptions? : "is objc_exception_thrownot an exception. This is a function that throws Objective-C exceptions. Similarly, EXC_ARITHMETICit is not an Objective-C exception, it is a Mach (kernel) exception, which means your application was trying to do something completely invalid. - Peter Hosei 14 May at 9:14 "

, , . http://www.restoroot.com/Blog/2008/10/20/crash-reporter-for-iphone-applications-part-2/, , , .

, , : http://www.openradar.me/8081169 ( )

(, .)

+3

.

Try/catch .

. , , . CATCH. finally .

+1

NSException, , NSInvalidArgumentException. , , .

NSArray * foo = [[NSArray alloc] init];

@try {
   NSLog(@"trying...");  
   [foo objectForKey:@"yeah"];
}
@catch (NSInvalidArgumentException *e) {
   NSLog(@"Invalid argument called.");
}
@catch (NSException * e) {
   NSLog(@"catching %@ reason %@", [e name], [e reason]);
}
@finally {
   NSLog(@"finally");
}

.

. http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocExceptionHandling.html.

+1

Iphone/Ipad @try @catch block .

0

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


All Articles