www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - scope block do not handle failure, but try-catch does

reply "Suliman" <evermind live.ru> writes:
string dbname = config.getKey("dbname1");
scope(failure) writeln("look like dbname is missing");

I am using dini and trying to throw exception if value can't be 
extract from config. If I am wrap it's in try-сефср block it's 
work or. But in this situation scope block do not execute and I 
see only stack tracing error on console.

Why? What's wrong. By idea if block failure scope should execute
Dec 11 2014
parent reply "Michael" <triorph gmail.com> writes:
On Thursday, 11 December 2014 at 20:40:40 UTC, Suliman wrote:
 string dbname = config.getKey("dbname1");
 scope(failure) writeln("look like dbname is missing");

 I am using dini and trying to throw exception if value can't be 
 extract from config. If I am wrap it's in try-сефср block it's 
 work or. But in this situation scope block do not execute and I 
 see only stack tracing error on console.

 Why? What's wrong. By idea if block failure scope should execute
I'm not 100% sure on this, but I suspect you have to declare the scope(failure) before you call the code that might execute it. e.g. scope(failure) writeln("Looks like the dbname is missing"); string dbname = config.getKey("dbname1");
Dec 11 2014
parent reply "Suliman" <evermind live.ru> writes:
If I right understand scope is not good for checking if one of 
function is fail.
For example:

string dbpass = config.getKey("dbpass");
string dbpass = config.getKey("dbpass");
string dbhost = config.getKey("dbhost");
string dbport = config.getKey("dbport");

if I will try to add scope(failure) writeln("bla-bla-bla") after 
every function I will execute all writeln("bla-bla-bla") even if 
second function, for example, are failed.

Is there any other way to do not wrap all function in try-catch 
block, but check if they was failed in short form?
Dec 13 2014
parent reply "Suliman" <evermind live.ru> writes:
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);
}
Dec 13 2014
parent reply drug <drug2004 bk.ru> writes:
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
Dec 14 2014
parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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.
Dec 15 2014
parent drug <drug2004 bk.ru> writes:
On 15.12.2014 12:22, "Marc Schütz" <schuetzm gmx.net>" wrote:
 Unfortunately you don't have access to the exception object inside the
 `scope(failure)` block.
Ah, yes, it has to be without msg.msg scope(failure) writeln("Something is wrong");
Dec 15 2014