www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Unreachable statement, do or do not, there is no try

reply "deadalnix" <deadalnix gmail.com> writes:
DMD accept both :
int foo() {
   try {
     throw new Exception("blah");
   } catch(Exception e) {
     return 25;
   }
}

and

int foo() {
   try {
     throw new Exception("blah");
   } catch(Exception e) {
     return 25;
   }

   return 42;
}

Which is it ? What are the rule for the unreachable statment ? It 
seems that DMD is capable of figuring out that the try never 
returns, or it would complain about missing return in the first 
case. But that do not seems consistent with not triggering the 
unreachable statement in case 2.

I'm working on improving control flow analysis in SDC. My 
handling of try will error on case 2. Is that the right thing ?

Also, if it is possible to detect that a statement finishes, 
without triggering a unreachable statement error, is that 
possible to make it so for static ifs ?
Feb 09 2015
parent reply Jacob Carlborg <doob me.com> writes:
On 2015-02-09 20:00, deadalnix wrote:
 DMD accept both :
 int foo() {
    try {
      throw new Exception("blah");
    } catch(Exception e) {
      return 25;
    }
 }

 and

 int foo() {
    try {
      throw new Exception("blah");
    } catch(Exception e) {
      return 25;
    }

    return 42;
 }

 Which is it ? What are the rule for the unreachable statment ? It seems
 that DMD is capable of figuring out that the try never returns, or it
 would complain about missing return in the first case. But that do not
 seems consistent with not triggering the unreachable statement in case 2.
DMD will complain about the second example if warnings are enabled. -- /Jacob Carlborg
Feb 09 2015
parent reply "deadalnix" <deadalnix gmail.com> writes:
On Tuesday, 10 February 2015 at 07:01:18 UTC, Jacob Carlborg 
wrote:
 DMD will complain about the second example if warnings are 
 enabled.
Ok I think that answer the question.
Feb 09 2015
parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 2/9/2015 11:07 PM, deadalnix wrote:
 On Tuesday, 10 February 2015 at 07:01:18 UTC, Jacob Carlborg wrote:
 DMD will complain about the second example if warnings are enabled.
Ok I think that answer the question.
The thing is, you can still catch an Object, so catching Exception won't quite catch them all.
Feb 10 2015
next sibling parent "Matthias Bentrup" <matthias.bentrup googlemail.com> writes:
On Tuesday, 10 February 2015 at 20:37:16 UTC, Walter Bright wrote:
 On 2/9/2015 11:07 PM, deadalnix wrote:
 On Tuesday, 10 February 2015 at 07:01:18 UTC, Jacob Carlborg 
 wrote:
 DMD will complain about the second example if warnings are 
 enabled.
Ok I think that answer the question.
The thing is, you can still catch an Object, so catching Exception won't quite catch them all.
s/Object/Error/ ?
Feb 10 2015
prev sibling next sibling parent reply "deadalnix" <deadalnix gmail.com> writes:
On Tuesday, 10 February 2015 at 20:37:16 UTC, Walter Bright wrote:
 On 2/9/2015 11:07 PM, deadalnix wrote:
 On Tuesday, 10 February 2015 at 07:01:18 UTC, Jacob Carlborg 
 wrote:
 DMD will complain about the second example if warnings are 
 enabled.
Ok I think that answer the question.
The thing is, you can still catch an Object, so catching Exception won't quite catch them all.
yes, but if you don't catch, then you continue unwinding, so whatever road you take, there is no way you reach whatever comes after the try.
Feb 10 2015
parent Walter Bright <newshound2 digitalmars.com> writes:
On 2/10/2015 12:48 PM, deadalnix wrote:
 On Tuesday, 10 February 2015 at 20:37:16 UTC, Walter Bright wrote:
 On 2/9/2015 11:07 PM, deadalnix wrote:
 On Tuesday, 10 February 2015 at 07:01:18 UTC, Jacob Carlborg wrote:
 DMD will complain about the second example if warnings are enabled.
Ok I think that answer the question.
The thing is, you can still catch an Object, so catching Exception won't quite catch them all.
yes, but if you don't catch, then you continue unwinding, so whatever road you take, there is no way you reach whatever comes after the try.
I agree the flow analysis code (which is pretty simplistic) should be looked at again. You should file a bugzilla.
Feb 10 2015
prev sibling next sibling parent reply Jonathan M Davis via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Tuesday, February 10, 2015 12:36:50 Walter Bright via Digitalmars-d wrote:
 On 2/9/2015 11:07 PM, deadalnix wrote:
 On Tuesday, 10 February 2015 at 07:01:18 UTC, Jacob Carlborg wrote:
 DMD will complain about the second example if warnings are enabled.
Ok I think that answer the question.
The thing is, you can still catch an Object, so catching Exception won't quite catch them all.
Wait, what? I thought that something had to be a Throwable to be throwable or catchable. Allowing any Object to be thrown/caught stinks of the C++ nonsense of allowing _anything_ to be thrown/caught. It's slightly better, because it's restricted to Objects and stuff like arrays and ints wouldn't count, but it would still be pretty horrible to be throwing Objects that aren't intended to be used as exception types. And anything that isn't derived from Exception being thrown could result in cleanup code not being run. If something other than a Throwable can currently be thrown, I would hope that we'd fix it so that that's no longer the case. - Jonathan M Davis
Feb 10 2015
parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 2/10/2015 7:49 PM, Jonathan M Davis via Digitalmars-d wrote:
 Wait, what? I thought that something had to be a Throwable to be throwable
 or catchable.
Checking the code, I see it now requires Throwable. (D1 required only Object.)
Feb 10 2015
next sibling parent "H. S. Teoh via Digitalmars-d" <digitalmars-d puremagic.com> writes:
On Tue, Feb 10, 2015 at 09:00:33PM -0800, Walter Bright via Digitalmars-d wrote:
 On 2/10/2015 7:49 PM, Jonathan M Davis via Digitalmars-d wrote:
Wait, what? I thought that something had to be a Throwable to be
throwable or catchable.
Checking the code, I see it now requires Throwable. (D1 required only Object.)
I'm sure glad we moved away from that!! T -- Once the bikeshed is up for painting, the rainbow won't suffice. -- Andrei Alexandrescu
Feb 10 2015
prev sibling parent Jonathan M Davis via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Tuesday, February 10, 2015 21:25:51 H. S. Teoh via Digitalmars-d wrote:
 On Tue, Feb 10, 2015 at 09:00:33PM -0800, Walter Bright via Digitalmars-d
wrote:
 On 2/10/2015 7:49 PM, Jonathan M Davis via Digitalmars-d wrote:
Wait, what? I thought that something had to be a Throwable to be
throwable or catchable.
Checking the code, I see it now requires Throwable. (D1 required only Object.)
Thanks!
 I'm sure glad we moved away from that!!
Agreed. - Jonathan M Davis
Feb 10 2015
prev sibling parent "Idan Arye" <GenericNPC gmail.com> writes:
On Tuesday, 10 February 2015 at 20:37:16 UTC, Walter Bright wrote:
 On 2/9/2015 11:07 PM, deadalnix wrote:
 On Tuesday, 10 February 2015 at 07:01:18 UTC, Jacob Carlborg 
 wrote:
 DMD will complain about the second example if warnings are 
 enabled.
Ok I think that answer the question.
The thing is, you can still catch an Object, so catching Exception won't quite catch them all.
Why does it matter in that example? If a non-Exception Throwable is thrown the return after the try..catch will still not be reached.
Feb 12 2015