Thread overview |
---|
December 11, 2014 scope block do not handle failure, but try-catch does | ||||
---|---|---|---|---|
| ||||
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 |
December 11, 2014 Re: scope block do not handle failure, but try-catch does | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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");
|
December 13, 2014 Re: scope block do not handle failure, but try-catch does | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michael | 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? |
December 13, 2014 Re: scope block do not handle failure, but try-catch does | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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); } |
December 15, 2014 Re: scope block do not handle failure, but try-catch does | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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
|
December 15, 2014 Re: scope block do not handle failure, but try-catch does | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug | 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.
|
December 15, 2014 Re: scope block do not handle failure, but try-catch does | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | 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");
|
Copyright © 1999-2021 by the D Language Foundation