views:

101

answers:

6

What’s the rationale behind the Cocoa exception policy - or why use exceptions only for programmer errors?

I understand that exception used to be rather expensive so one would not want to overuse them. But that changed with the modern runtime and it’s zero-cost exceptions. I also understand that the use of exceptions to do general control flow is not a good idea because it could lead to code that is rather hard to understand.

But why should one use exceptions to signal programmer errors? For that case logging a message followed by abort() should be enough. Why should I write a @catch(...) block to handle a programmer error instead of fixing the actual mistake? I’ve been thinking about this a lot and I haven’t found any reasonable use of an exception for a programmer error.

(As a side note/question: I’ve written a recursive descent parser, and I’m planning on using exceptions in there for handling errors. Seems to be much more reasonable to me than adding an out parameter to every single function in there and manually check for an error everywhere. Of course I’ll catch any exceptions I throw in the top level methods that get called from the outside. Anyone think that’s a bad use for exceptions?)

Update: The real question

Thanks for all the answers so far. They all are true, but they don’t actually answer my question. So I guess I wasn’t really clear about it, sorry for that. So here’s the real question:

Why does Cocoa throw exceptions for programmer errors (or assertions) at all? One isn’t supposed to catch them, and actually writing code that handles a programmer error somewhere down the call stack is not a good idea anyways. Seems to me that exceptions there are a wasted effort. Simply logging the error and calling abort() (which exits the program) should be enough. So what’s the advantage there of actually having an exception thrown?

I understand why exceptions are not generally used and discouraged - most parts of Cocoa are just not exception safe. And that’s not the question here. I hope I made this clear now.

A: 

The modern runtime does not give you zero-cost exceptions, it gives you exceptions that only incur their cost if an exception is thrown.

NSResponder
Yes, I know. There is no cost for the try/catch block. Throwing an exception is supposed to be even more expensive than before. But I didn’t invent the name zero-cost exceptions, that’s what Apple calls this feature.
Sven
+3  A: 

Why should I write a @catch(...) block to handle a programmer error instead of fixing the actual mistake?

In most cases, you wouldn't. In Objective-C, you generally don't handle exceptions. If an exception occurs, it causes a crash, and then you fix the bug -- hopefully you catch this during testing.

Of course, in some cases this doesn't work out. Maybe you do except an exception and you can workaround it, so you catch it. Or there's there rare API that'll throw exceptions instead of using error objects.

To be honest, I very, very rarely use try/catch in my Objective-C code.

As for the rationale, I think it's largely due to Objective-C's C heritage. Back in the early 80s when Objective-C was developed, exceptions were kind of "new" (i.e., not in many mainstream languages yet), and Objective-C catered more to the C tradition of using NULL or an out parameter to signal errors.

mipadi
I know, that is what the documentation says. What I’d like to know is why exceptions are used at all by Cocoa if you are not supposed to handle them. If you’re not supposed to handle the exception why bother with actually throwing one? `abort()` is so much simpler and more efficient.
Sven
@Sven: In Objective-C, the idea is to use errors to signal problems that are plausible: a failed network connection, an inaccessible disk, that sort of thing. Exceptions are "exceptional" and are used to indicate errors that the programmer should check for himself -- things like array out of bounds exceptions, for instance.
mipadi
@mipadi, I understand that and you don’t need to repeat Apple’s documentation at me - I’ve read that myself. The question is about the **why**.
Sven
@Sven: I gave a reason as to "why" in my answer. To expand, I think that exceptions were added because it was expected that a language should have exceptions, but due to its C heritage they weren't used that much. Exceptions weren't even a part of the language originally, and GCC didn't support Objective-C exceptions until v3.3. Even now, you have to enable Objective-C exceptions with a flag.
mipadi
To add: Previously, exceptions were purely a Cocoa thing—you could only throw an NSException object, you threw it by sending it a `raise` message, and try and catch were implemented as macros declared in the headers.
Peter Hosey
+1  A: 

The main reason for avoiding throwing exceptions is that you may accidentally throw them through stack frames that are not exception aware. For instance, if a data source for a table view throws an exception, that is not caught and handled before the delegate method returns control to the table view, it might cause all sorts of trouble as it unwinds the table view's stack frames, side stepping various releases of temporary objects and other resources.

Having said that, I personally like exceptions and use them wherever I think they are the natural thing to do, but with the caveat of never allowing them to propagate to code that is not documented as exception aware.

JeremyP
+1  A: 

There are likely a lot of reasons. The "historical reasons" others have covered is sufficient to explain the current state of affairs, but there are other possibilities.

Another possibility is Objective C is not typically a "Resource Acquisition Is Initialization" kind of language (yes this is more a library issue then a language issue, but it is real). So most Objective C code that has an error thrown through it will leave invalid program state (things still locked, over retained objects). All things you could deal with if you were thinking about it, and not all things RAII would magically fix (there is a lot of exception unsafe C++ code out there, and C++ is largely RAII).

As noted above stating that you do handle an exception is free(ish), but actually having one thrown is costly (maybe an order of magnitude or two more costly then an extra parameter and a conditional check). So if your parser (for example) uses them to signal errors in parsing, being given a document with a lot of errors can take a LOT longer to parse then if you had explicit checks for an error parameter.

Personally I like exceptions, and would prefer to throw exceptions from my libraries when things "go wrong", but that isn't the Cocoa way, so I use exceptions to handle programmer errors and an error indication and NSError** for other things. It isn't great, but it makes it so other people can use my libraries without having to learn a new way to write Objective C code.

Stripes
+1  A: 

Why does Cocoa throw exceptions for programmer errors (or assertions) at all? One isn’t supposed to catch them, and actually writing code that handles a programmer error somewhere down the call stack is not a good idea anyways

Ah!

Three reasons leap to mind.

One, if you catch an exception more or less at your main run loop you could autosave state to a temporary location, crash, and on restart have a "try to restore from just before the crash, warning: may cause another crash and you should check your data very carefully" dialog/sheet/thingie. Or even just catch the exception and tell the user to do a "Save As", quit and restart.

Two, things like the unit test framework make good use of exceptions to abort the current test (logging a failure), and continuing with the rest of the tests. This lets you see if a change you made has one regression (that happens to index a NSArray out of bounds), or if you have six regressions (one or more of which throw an exception).

Three, maybe when added to ObjC it was intended to handle many kinds of errors with exceptions, and after real world experience the useful scope was determined to be "nearly fatal errors only".

Stripes
+1  A: 

Your question explicitly assumes that "one isn’t supposed to catch them." This is incorrect. The programmer isn't expected to catch them under normal circumstances, but that isn't to say that they must never be caught for any purpose.

Example: I'm not sure if it does anymore since it's much less buggy these days, but I know it at least used to be the case that Xcode would catch exceptions and put up a dialog saying, "Such-and-such happened. It doesn't appear to be a critical problem, but you should probably save and restart the program to avoid any trouble in the future."

Chuck
I see what you mean. Xcode still does that to me sometimes.
Sven