#catch not catching - iphone

In the code below I'm trying to catch an exception that is being thrown because the delegate doesn't implement the boolEntryCellViewControllerSegmentChanged method. However, the exception doesn't seem to be getting caught, the program is still crashing with an "unrecognized selector sent to instance" error. I'd be grateful for any pointers as to what I'm doing wrong.
#try
{
[delegate boolEntryCellViewControllerSegmentChanged:self];
}
#catch (NSException *exception)
{
NSLog(#"why doesn't this work?");
}
#finally
{
NSLog(#"why, why why?");
}

This is probably due to a known bug: http://www.openradar.me/8081169
In most cases, this only happens on the simulator. On the device itself, it usually works. But YMMV.

Related

how to catch exception thrown by imageWithData: iPhone

I want to try to create an image using NSdata. If imageWithData: method of UIImage can create it successfully, i will follow a path but if it cannot create i want to follow another way.
Is this even possible?
I tried
#try {
im = [UIImage imageWithData:data];
NSLog(#"Trying");
}
#catch (NSException * e) {
NSLog(#"Exception");
anotherData = doSomethingWithData(data)
im = [UIImage imageWithData:anotherData];
}
#finally {
NSLog(#"Final");
[self.questionList addObject:im];
}
but it causes app to crash.
How can I catch this exception without causing app to crash?
Exception is this:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 0'
Code does not create image from data but still it does not throws an exception or anything about it. Is there a way that I can understand if image is created or not.
You get the error, because im remains nil and you are not allowed to add nil to an array.
You could do it like that:
im = [UIImage imageWithData:data];
if(im == nil) {
anotherData = doSomethingWithData(data)
im = [UIImage imageWithData:anotherData];
}
if(im != nil)
[self.questionList addObject:im];
An implementation similar to your pattern above would be to re-write the finally block like such:
#finally {
NSLog(#"Final");
if(im) {
[self.questionList addObject:im];
}
}
However the entire piece of code could easily be re-written (and should) to not require exception handling.

How to rectify the crash related to iAd in my application

I have integrated iAd in my application and i am executiong it in my simulator. I kept the target iOS 4.1. But some times I am getting the error:
"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString bannerViewDidLoadAd:]: unrecognized selector sent to instance 0x6451cd0' " and the application will be quit. What i have to do.
I statically added the ADBannerView in interface builder and used the delegate methods like this:
#pragma mark ADBannerViewDelegate
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
Class classAdBannerView = NSClassFromString(#"ADBannerView");
if(classAdBannerView!= nil)
iAdView2.hidden = NO;
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
Class classAdBannerView = NSClassFromString(#"ADBannerView");
if(classAdBannerView!= nil)
iAdView2.hidden = YES;
}
If any one know the solution please help me.
Exception:
NSInvalidArgumentException', reason:
'-[NSCFString bannerViewDidLoadAd:]: unrecognized selector sent to instance 0x6451cd0' "
Suggests that your delegate object going out of scope. It may be because you have created it in auto-release pool or releasing it somewhere. You will need to retain it.
Thanks

well coded exception handling - #try #catch or without it

I want to know the global mechanism for exception handling.
Like I do have 20 different classes and I want to put try catch in all , then It may be some what time consuming as well as may can create issues.
I found some code like
-(void)ExceptionOccure:(NSException *)exception
{
NSArray *stack = [exception callStackReturnAddresses];
NSLog(#"%#", stack);
}
But I am not able to call this method , using
-(void) applicationDidFinishLaunching: (UIApplication *) application
{
NSSetUncaughtExceptionHandler (&ExceptionOccure);
}
I came to know this way but not able to call any exception.
I want to create global mechanism for this exception handling. so that in any class if any exception occurs it will call the above written function and handle the exception as well as can store the stack in the database as I needed.
Can any one suggest me or provide me sample code for handling exception globally..
thanks in advance
your handler function is a C function so you have to implement it like this :
static void uncaughtExceptionHandler(NSException *exception)
{
NSArray *stack = [exception callStackReturnAddresses];
NSLog(#"%#", stack);
}
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
}

try & catch in iPhone?

The try{}catch construct is common to C++, Java & related languages. In the iOS SDK is there and any functionality like this?
#try {
// Try something
}
#catch (NSException * e) {
NSLog(#"Exception: %#", e);
}
#finally {
// Added to show finally works as well
}

Sigabrt on Main function

I've just began to learn iOS development and now I have a sigabrt on my main function. The error is triggered by calling:
int retVal = UIApplicationMain(argc, argv, nil, nil);
As I am very new to iOS programming I have no idea on what could by causing this error. One thing that I have found on my callstack is the following exception being raised: dyld_stub_objc_exception_throw
What could be causing this error?
I was having this problem in X-Code 4.2 and the issue was it couldn't find my Storyboard. The frustrating part was that there was no indication of what the actual exception was. I was able to get the exception in the log by adding a try / catch to my main function. (Note I'm using ARC, so if you aren't using ARC your main will look a little different with try catch)
int main(int argc, char *argv[])
{
int retVal = 0;
#autoreleasepool {
NSString *classString = NSStringFromClass([sortaAppDelegate class]);
#try {
retVal = UIApplicationMain(argc, argv, nil, classString);
}
#catch (NSException *exception) {
NSLog(#"Exception - %#",[exception description]);
exit(EXIT_FAILURE);
}
}
return retVal;
}
UIApplicationMain basically kicks off the rest of your application, so the actual cause of the crash could be anywhere in your application. You may need to look further down the call stack.
However, I note that you're passing nil as the final two parameters. The first nil is the principalClassName argument, which can legally be nil if you want UIApplication to be the principal class. The second nil is the application delegate name; you should pass nil if you load the delegate from the main nib. Do you? If you don't, that might be the problem (I can't say I've ever called this function with both of those arguments nil).
It was just a typical error of someone learning a new language/API. I forgot to properly set up the view.
UIView *controllersView = [myViewController view];
[window addSubview:controllersView];