try/catch idiom in std.datetime

Jonathan M Davis jmdavisProg at gmx.com
Mon Nov 18 03:03:47 PST 2013


On Monday, November 18, 2013 11:53:50 Jacob Carlborg wrote:
> On 2013-11-18 11:16, Daniel Murphy wrote:
> > Yeah.  On the other hand, if we decide assert(0) means 'assume
> > unreachable'
> > we can optimize out the try-catch in release mode, among other things.
> > 
> > try { s } catch { assert(0); } -> s
> > if (e) assert(0); else s; -> e; s;
> 
> "assume" isn't the same as the compile will do this. Currently the spec
> says "Either AssertError is thrown at runtime if it is reachable, or the
> execution is halted". To me that means an implementation is free to
> throw an AssertError.

assert(0) is intended specifically for use in cases where a line is supposed to 
be unreachable, and it wouldn't make any sense to use it in any other case, 
because assertion failures are intended to kill the program, and assert(0) 
always fails. It happens that it throws an AssertError in non-release mode in 
order to give you better debug information on failure, and it's a HLT 
instruction in release, but in either case, it's intended to be unreachable 
code.

The spec really should be updated to make it clear that when assertions are 
compiled in, assert(0) throws an AssertError and that when assertions are 
supposed to be compiled out, it becomes a HLT instruction. And if need be, we 
can update the spec to require that try-catches be compiled out when 
assertions are compiled out, and the catch's body only contains an assert(0).

- Jonathan M Davis


More information about the Digitalmars-d mailing list