scope block do not handle failure, but try-catch does

drug via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Dec 14 23:41:37 PST 2014


On 13.12.2014 23:26, Suliman wrote:
> I reread docs and understood that scope not for such case.
>
> Next code is do what I need:
> try
> {
> string dbname = config.getKey("dbname");
> string dbpass = config.getKey("dbpass");
> string dbhost = config.getKey("dbhost");
> string dbport = config.getKey("dbport");
> }
>
> catch (Exception msg)
> {
> writeln("Can't parse config: %s", msg.msg);
> }
What you need probably is the following:

string dbpass, dbhost, dbport;

{ // This bracket is intended to define new scope
	// New if in the current scope from now any failure occurs
	// the compiler will call writeln with message
	scope(failure) writeln("Can't parse config: %s", msg.msg);

	dbname = config.getKey("dbname");
	dbpass = config.getKey("dbpass");
	dbhost = config.getKey("dbhost");
	dbport = config.getKey("dbport");
} // the current scope ends, so if a failure happens later writeln won't 
be called


More information about the Digitalmars-d-learn mailing list