Throwable, Exception, and Error

Steve Teale steve.teale at britseyeview.com
Sun May 3 23:45:31 PDT 2009


Jarrett Billingsley Wrote:

> On Sun, May 3, 2009 at 4:14 PM, Denis Koroskin <2korden at gmail.com> wrote:
> > PHP allows function definition like this:
> >
> > void foo(Args args, string fileName = __FILE__, int line = __LINE__)
> > {
> >  // do stuff
> > }
> 
> Hidden feature: D2 allows it too, at least with templates.
> 
> void Throw(T : Throwable, string file = __FILE__, int line = __LINE__)(T ex)
> {
> 	ex.file = file;
> 	ex.line = line;
> 	throw ex;
> }
> 
> void foobar()
> {
> 	Throw(new Exception("o hai"));
> }
> 
> void main()
> {
> 	try
> 		foobar();
> 	catch(RangeError e)
> 		writefln("(%s: %d) %s", e.file, e.line, e.msg);
> }
> 
> I did find a sort of bug when trying a slightly different
> implementation of this though - any kind of explicit instantiation of
> the Throw template causes the __FILE__ and __LINE__ to be evaluated in
> the scope of the template rather than at the call site.

Sadly this is a subterfuge that makes it impossible as far as I can see to write code that compiles and works the same with both D1 and D2.

D1 compiles it, but reports the exception at the line where the template function is defined.

;=(




More information about the Digitalmars-d mailing list