I just noticed that the new programming language D just “officially” went stable a few days ago. So, I decided to finally get around to reading through the language definition. There are some interesting things in there that look useful (though some of potentially perl-esque arcaneness.) But exception handling appears to use a new (to me) paradigm that looks very nice.
http://www.digitalmars.com/d/exception-safe.html
Traditional exception handling allowed programmers to be able to return an error from functions without impairing the possible return results, or necessitating you to look up what–for each function–constituted an error value or a success. It, however, didn’t address the issue of having to do individual error checks for each function call, and cleaning up after functions which succeeded previous to the error. Coding proper error handling still resulted in ugly, multi-nested code that was a hastle to create and rarely called; meaning that coders tended to just not do it.
D solves this by letting you establish a roll-back that cleans up everything previous to the error when the scope is exited.
For instance, if you were doing the init section of an app, it could look like this:
void init() {
hash.init();
scope(failure) hash.cleanup();
db.connect();
scope(failure) db.disconnect();
logger.open();
}
If logger.open() throws an exception, init() doesn’t just pass the exception handler up to the caller, it rolls back through all scope(failure) declarations up to that point and runs them. db.disconnect() will be called, then hash.cleanup(). If db.connect() threw an error, then just hash.cleanup() will be called.
Very cool. I’m going to give the language some playing about.