The Right Approach to Exceptions

Sean Kelly sean at invisibleduck.org
Sun Feb 19 08:42:56 PST 2012


I think it depends on the application. For user apps where errors may often simply be reported, type probably doesn't matter. For apps that may want to change their behavior based on certain errors, catching by type can be useful. Also, testing in/out contracts for correctness pretty much relies on exceptions differentiated by type. 

As one simple example of where I've found throwing by type useful, I've written back-end apps that interact with a DB. If a DB connection occurs during processing I need to tear down an array of processing functionality and attempt reconnects in a progressive back-off to avoid spinning on reconnect. Throwing just Exception instead of DbException or whatever means a lot of casting or other inspection to find the useful info and re-throwing if I don't care about the exception. If I'm going to do that I pretty much may as well go back to C-style programming and checking return values. 

On Feb 18, 2012, at 10:52 AM, Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:

> There's a discussion that started in a pull request:
> 
> https://github.com/alexrp/phobos/commit/4b87dcf39efeb4ddafe8fe99a0ef9a529c0dcaca
> 
> Let's come up with a good doctrine for exception defining and handling in Phobos. From experience I humbly submit that catching by type is most of the time useless.
> 
> 
> Andrei


More information about the Digitalmars-d mailing list