Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 977095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:50:56+00:00 2026-05-16T03:50:56+00:00

The Problem I’m writing a Cocoa application and I want to raise exceptions that

  • 0

The Problem

I’m writing a Cocoa application and I want to raise exceptions that will crash the application noisily.

I have the following lines in my application delegate:

[NSException raise:NSInternalInconsistencyException format:@"This should crash the application."];
abort();

The problem is, they don’t bring down the application – the message is just logged to the console and the app carries on it’s merry way.

As I understand it, the whole point of exceptions is that they’re fired under exceptional circumstances. In these circumstances, I want the application to quit in an obvious way. And this doesn’t happen.

What I’ve tried

I’ve tried:

-(void)applicationDidFinishLaunching:(NSNotification *)note
    // ...
    [self performSelectorOnMainThread:@selector(crash) withObject:nil waitUntilDone:YES];
}

-(void)crash {
    [NSException raise:NSInternalInconsistencyException format:@"This should crash the application."];
    abort();
}

which doesn’t work and

-(void)applicationDidFinishLaunching:(NSNotification *)note
    // ...
    [self performSelectorInBackground:@selector(crash) withObject:nil];
}

-(void)crash {
    [NSException raise:NSInternalInconsistencyException format:@"This should crash the application."];
    abort();
}

which, rather confusingly, works as expected.

What’s going on? What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:50:57+00:00Added an answer on May 16, 2026 at 3:50 am

    UPDATE – Nov 16, 2010: There are some issues with this answer when exceptions are thrown inside IBAction methods. See this answer instead:

    How can I stop HIToolbox from catching my exceptions?


    This expands on David Gelhar’s answer, and the link he provided. Below is how I did it by overriding NSApplication’s -reportException: method. First, create an ExceptionHandling Category for NSApplication (FYI, you should add a 2-3 letter acronym before “ExceptionHandling” to reduce the risk of name clashing):

    NSApplication+ExceptionHandling.h

    #import <Cocoa/Cocoa.h>
    
    @interface NSApplication (ExceptionHandling)
    
    - (void)reportException:(NSException *)anException;
    
    @end
    

    NSApplication+ExceptionHandling.m

    #import "NSApplication+ExceptionHandling.h"
    
    @implementation NSApplication (ExceptionHandling)
    
    - (void)reportException:(NSException *)anException
    {
        (*NSGetUncaughtExceptionHandler())(anException);
    }
    
    @end
    

    Second, inside NSApplication’s delegate, I did the following:

    AppDelegate.m

    void exceptionHandler(NSException *anException)
    {
        NSLog(@"%@", [anException reason]);
        NSLog(@"%@", [anException userInfo]);
    
        [NSApp terminate:nil];  // you can call exit() instead if desired
    }
    
    - (void)applicationWillFinishLaunching:(NSNotification *)aNotification
    {
        NSSetUncaughtExceptionHandler(&exceptionHandler);
    
        // additional code...
    
        // NOTE: See the "UPDATE" at the end of this post regarding a possible glitch here...
    }
    

    Rather than use NSApp’s terminate:, you can call exit() instead. terminate: is more Cocoa-kosher, though you may want to skip your applicationShouldTerminate: code in the event an exception was thrown and simply hard-crash with exit():

    #import "sysexits.h"
    
    // ...
    
    exit(EX_SOFTWARE);
    

    Whenever an exception is thrown, on the main thread, and it’s not caught and destroyed, your custom uncaught exception handler will now be called instead of NSApplication’s. This allows you to crash your application, among other things.


    UPDATE:

    There appears to be a small glitch in the above code. Your custom exception handler won’t “kick in” and work until after NSApplication has finished calling all of its delegate methods. This means that if you do some setup-code inside applicationWillFinishLaunching: or applicationDidFinishLaunching: or awakeFromNib:, the default NSApplication exception handler appears to be in-play until after it’s fully initialized.

    What that means is if you do this:

    - (void)applicationWillFinishLaunching:(NSNotification *)aNotification
    {
            NSSetUncaughtExceptionHandler(&exceptionHandler);
    
            MyClass *myClass = [[MyClass alloc] init];   // throws an exception during init...
    }
    

    Your exceptionHandler won’t get the exception. NSApplication will, and it’ll just log it.

    To fix this, simply put any initialization code inside a @try/@catch/@finally block and you can call your custom exceptionHandler:

    - (void)applicationWillFinishLaunching:(NSNotification *)aNotification
    {
        NSSetUncaughtExceptionHandler(&exceptionHandler);
    
        @try
        {
            MyClass *myClass = [[MyClass alloc] init];   // throws an exception during init...
        }
        @catch (NSException * e)
        {
            exceptionHandler(e);
        }
        @finally
        {
            // cleanup code...
        }
    }
    

    Now your exceptionHandler() gets the exception and can handle it accordingly. After NSApplication has finished calling all delegate methods, the NSApplication+ExceptionHandling.h Category kicks in, calling exceptionHandler() through its custom -reportException: method. At this point you don’t have to worry about @try/@catch/@finally when you want exceptions to raise to your Uncaught Exception Handler.

    I’m a little baffled by what is causing this. Probably something behind-the-scenes in the API. It occurs even when I subclass NSApplication, rather than adding a category. There may be other caveats attached to this as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I have two spreadsheets that each serve different purposes but contain one particular
Problem: We have a web app that calls some web services asynchronously (from the
Problem: I have an address field from an Access database which has been converted
Problem (simplified to make things clearer): 1. there is one statically-linked static.lib that has
Problem: Ajax suggest-search on [ n ] ingredients in recipes. That is: match recipes
Problem is described and demonstrated on the following links: Paul Stovell WPF: Blurry Text
Problem I have timestamped data, which I need to search based on the timestamp
Problem solved: Thanks guys, see my answer below. I have a website running in
Problem: Given a list of strings, find the substring which, if subtracted from the
Problem Language: C# 2.0 or later I would like to register context handlers to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.