• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSExceptionHandler question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSExceptionHandler question


  • Subject: Re: NSExceptionHandler question
  • From: Gideon King <email@hidden>
  • Date: Thu, 11 Jul 2002 08:27:50 +0800

You can do something like:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *stack = [[exception userInfo] objectForKey:NSStackTraceKey];
NSTask *task = [[NSTask alloc] init];
NSString *pid = [[NSNumber numberWithInt:getpid()] stringValue];
NSMutableArray *args = [NSMutableArray array];
NSPipe *newPipe = [NSPipe pipe];
NSFileHandle *readHandle = [newPipe fileHandleForReading];
NSData *inData = nil;
[args addObject:@"-p"];
[args addObject:pid];
[args addObjectsFromArray:[stack componentsSeparatedByString:@" "]];

[task setStandardOutput:newPipe];
[task setLaunchPath:@"/usr/bin/atos"];
[task setArguments:args];
[task launch];

while ((inData = [readHandle availableData]) && [inData length])
{
NSLog(@"%@", [[[NSString alloc] initWithData:inData encoding:NSASCIIStringEncoding] autorelease]);
}

[task release];
[pool release];

... as long as the stack trace is in your exception.

I have tried implementing an uncaught exception handler, by putting the call to NSSetUncaughtExceptionHandler() in my application's init method. If I do that, an exception thrown in my application object is caught and the above code prints the backtrace, but not if the exception is thrown elsewhere. Also it I try to establish my uncaught exception handler elsewhere in my code, it doesn't work. I would really appreciate it if someone could shed some light on this, as it has me stumped.

Gideon.

On Thursday, July 11, 2002, at 12:59 AM, Mike Laster wrote:

How do I get the backtrace capabiltiy of NSExceptionHandler yet still be
able to set my own uncaught exception handler?

Here is some example code:


#import <Foundation/Foundation.h>
#import <ExceptionHandling/NSExceptionHandler.h>

void _uncaughtExceptionHandler(NSException *e)
{
NSLog(@"Handle uncaught exception here: %@ (%@)", e, [e userInfo]);
}

int main (int argc, const char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSSetUncaughtExceptionHandler((NSUncaughtExceptionHandler
*)_uncaughtExceptionHandler);

[[NSExceptionHandler defaultExceptionHandler] setExceptionHandlingMask:
NSHandleUncaughtExceptionMask|
NSHandleUncaughtSystemExceptionMask|
NSHandleUncaughtRuntimeErrorMask|
NSHandleTopLevelExceptionMask|
NSHandleOtherExceptionMask
];

// insert your code here

NS_DURING
[NSException raise:NSGenericException format:@"Test Exception"];
NS_HANDLER
NSLog(@"localException: %@: %@", localException, [localException
userInfo]);
NS_ENDHANDLER

[NSException raise:NSGenericException format:@"Test Uncaught
Exception"];

[pool release];
exit(0); // insure the process exit status is 0
return 0; // ...and make main fit the ANSI spec.
}


The caught exception works fine, it has backtrace info in it like I want.
However the uncaught exception doesn't have one. What do I need to do to
get a backtrace in EVERY exception, caught our uncaught?
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >NSExceptionHandler question (From: Mike Laster <email@hidden>)

  • Prev by Date: Re: NSBrowser bug?
  • Next by Date: Re: NSBrowser bug?
  • Previous by thread: NSExceptionHandler question
  • Next by thread: Plists and booleans
  • Index(es):
    • Date
    • Thread