TryElseExpression DIP
pineapple via Digitalmars-d
digitalmars-d at puremagic.com
Mon Sep 5 11:07:52 PDT 2016
I was writing some code and realized D doesn't have an equivalent
to Python's `else` for error handling, and I think we should
change that
https://github.com/dlang/DIPs/pull/43/files
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
Imitating this functionality in D,
try{
do_a_thing();
}catch(Exception exception){
handle_error();
}else{
depends_on_success_of_thing();
}finally{
do_this_always();
}
Would be equivalent to
bool success = false;
try{
do_a_thing();
success = true;
}catch(Exception exception){
handle_error();
}finally{
try{
if(success){
depends_on_success_of_thing();
}
}finally{
do_this_always();
}
}
More information about the Digitalmars-d
mailing list