On Tuesday, 27 September 2016 at 09:48:42 UTC, Jonathan M Davis
wrote:
On Tuesday, September 27, 2016 09:30:10 Dicebot via
Digitalmars-d wrote:
https://github.com/dlang/DIPs/blob/master/DIPs/DIP1002.md
PR: https://github.com/dlang/DIPs/pull/43
Abstract:
In Python, the try/catch/finally syntax is augmented with an
additional clause, termed else. It is a fantastically useful
addition to the conventional syntax. It works like this:
```
try:
do_something()
except Exception as e:
pass # Runs when an error inheriting from Exception
was
raised
else:
pass # Runs when no error was raised
finally:
pass # Runs unconditionally, evaluates last
```
And why not just put the code that would go in the else at the
end of the try block? Just like with this proposed else, the
code would only run if the preceding code didn't throw any
exceptions. This just seems like an attempt to make D more like
python rather than to add anything useful.
- Jonathan M Davis
Exceptions thrown in the `else` clause are not caught in the
catch/expect clauses. This gives you finer grained control:
try {
auto f1 = File("f1.txt");
} catch (ErrnoException) {
// f1.txt not found? no biggie...
} else {
// This won't happen if we can't open f1.txt
// If we can't open f2 we don't want to catch the
exception:
auto f2 = File("f2.txt", "w");
// Do stuff with f1 and f2
}
// This will still happen even if we can't open f1.txt
BTW, if this feature is ever implemented in D, it's important
that the else clause will continue the try clause's scope.