Register UncaughtExceptionHandler in Objective C using NSSetUncaughtExceptionHandler

Martin’s answer is correct. However, I thought I’d elaborate just a bit, to explain why it is so.

Your function definition:

void myHandler(NSException * exception)
{
  // ...
}

defines a function that will be externally visible. In other (generalized, non-technical) words, a symbol will be created in the object file so that the linker can find it, which allows other files to call myHandler.

However, because it is supposed to be externally visible, other files are going to have to know what that function looks like. That’s where the prototype comes into play. The warning is basically saying…

Hey, you have declared this function to be externally visible to other
code, but I don’t see a prototype that other code can use to know
about the function.

So, you get a warning. It’s a good warning to have on. It helps you remember to declare prototypes for functions that you want to export.

Now, as you discovered, you can declare a prototype, and the warning goes away. However, declaring the prototype just in the implementation file should be another warning to you. That personal warning should be:

Do you really want this function to have external visibility, or is it just called in this compilation unit? If the function is not going to have external visibility, then there is no need to export it in the symbol table, and there is no need for a prototype that other modules can include so they know about the function.

In that case, you can declare the function static as in Martin’s response:

static void myHandler(NSException * exception)
{
  // ...
}

In this context, static tells the compiler something like:

Hey, compiler, create code for this function, and allow any code in
this compilation unit to see the function, but do not give it external
visibility. I don’t want the function to be called by other modules.

In this case, even if other code declared the prototype, they would not see your function because it is “private” to the file in which it is defined.

Since it is being used only in the local file, there is no need for a prototype, so there is no need to warn you that you don’t have one.

Now, just as a note… You don’t need to put C functions in the @interface and @implementation sections of your code, as that does nothing. Those C functions are compiled with the exact same visibility and access whether they are inside the ObjC sections or not.

Finally, if you want, you can disable this warning in the Xcode build settings (but now that you understand the context of the warning, I suggest leaving it on).

Leave a Comment