Created attachment 389
Patch (DMD 2.030)
The proposed patch implements support for throw/try/catch/finally in CTFE. Throw statement is, however, somewhat limited; new expression is not allowed, except for new Exception("msg").
Example and output:
--------------------
int thrower(string s)
{
// The interpretor emulates throw new Exception("msg")
throw new Exception("exception " ~ s);
return 0;
}
int catcher()
{
try
{
return thrower("abc");
}
catch (Exception e)
{
throw e;
}
return 0;
}
enum a = catcher("abc");
--------------------
test.d(21): Error: uncaught exception from catcher(): "exception abc"
test.d(21): Error: cannot evaluate catcher() at compile time
--------------------
Comment #1 by rsinfu — 2009-06-04T15:37:11Z
Created attachment 391
Additional patch
Please apply this patch in addition to the first one. The first one allowed this problematic code:
--------------------
Exception foo()
{
Exception r;
try
{
throw new Exception("error");
}
catch (Exception e)
{
r = e;
}
return r;
}
enum Exception e = foo();
--------------------
This patch fix the first patch so that a CTFE exception object cannot be used as a usual object. The only allowed use of a CTFE exception object is this:
catch (Exception e) { throw e; }
Comment #2 by rsinfu — 2009-06-04T20:42:46Z
Created attachment 392
Patch: scope(success/failure/exit)
Supplemental patch to get scope guard statement working in CTFE.