Exceptions or status codes

Hi everyone,

I have been debating this matter for ages now, reading up on it, but I can't come to a decision.

I'm writing a library (for personal use) for web applications, which should be as efficient as possible. Consider a read() function as an example. It will read something from the database and it's contents are passed to a template.
Following things could go wrong in the read() function: a database error, the resource may not exist or may be empty, etc.

Now, I can't decide whether functions like read() should throw exceptions, or return error codes. I have read this, among may others: http://www.boost.org/community/error_handling.html Which asks "do you need stack unwinding"? No, I probably don't. But when a resource is not available, the information cannot be shown to the end user either, so it would be nice to "force" the caller to handle that accordingly. Another benefit of exceptions is that they can be thrown one or two levels deeper and handled in one common place. The "depth" from throw to catch should never be more than 2 or 3 levels I think, but might that be enough to cause a performance hit?

So I'm basically dealing with "status codes", not exceptional situations, so it might be dubbed an abuse of the exception capabilities?

As you may be able to tell, I'm completely lost - both sides have valid arguments and I can't decide - so I'd like to get your opinions on this.

Thanks very much in advance.

All the best,

Use status codes if the error handling is local (the caller of a function is expected to handle the error); exceptions if the error handling is non-local (the function has no clue as to where the error may get handled).
Hmm, interesting perspective, seems like a good point.

Anyone else who would like to add their view?

For a library, you could do what boost.asio and boost.filesystem did, and provide two versions of every API that can fail: throwing and non-throwing.
I've looked into the boost.asio documentation and I understand what you mean.
The system_error they use is pretty much what I would do for my status codes.

Thanks for sharing that info Cubbi, I had not considered the possibility of doing both yet.

Any more opinions?

All the best,
Just for future reference, if anyone is interested in this matter, here are a number of articles I read which convinced me to use exceptions.

My main consideration was performance, and these articles helped persuade me that performance comparison between the two methods is about more than saying "exceptions kill performance". The two methods need to be compared, that is, is there an extra performance penalty of try/catch versus an if-branch of status code checking. If so, is that penalty worth not having the benefits of exceptions. I'll let you decide for yourselves.


Thanks to both JLBorges and Cubbi for their input :)

All the best,
You may also want to have a look at the section on exceptions in TR 18015.
Ah yes, thanks very much for that!
I read it yesterday, but forgot where I found it.
It helped me to understand the first article I linked to better.

All the best,
Topic archived. No new replies allowed.