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

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Dec 15 01:22:02 PST 2014


On Monday, 15 December 2014 at 07:41:40 UTC, drug wrote:
> 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

Unfortunately you don't have access to the exception object 
inside the `scope(failure)` block.


More information about the Digitalmars-d-learn mailing list