D-Programming-Language/phobos — pull request #1090 by someboddy (2013-01-22 02:55:36+00:00)
Title: Add ml style exception handling

ML has a very elegant exception handling syntax. You can write:
```ml
hd [] handle Empty => "the list is empty"
```
and it'll return ```"the list is empty"``` - if there were any items in the list it would have returned the first item instead. Since there is no need to wrap the expression in a ```try``` block, this style remains elegant and readable even when you have a complex expression with multiple points where you want to catch errors.

It is possible to get something close in D using lazy arguments and function-method interchanging. I've added the ```handle``` function to ```std.exception``` that does just that:
```D
string[] arr=[];
writeln( arr[0].handle!RangeError("the array is empty") );
```

It's not as elegant as ML, since ML has it built in as a syntax and it has better type inference, but I've tried it in a project and it provided much more elegant code than the ```try```-```catch``` blocks it replaced.