Re: Throwable, Exception, and Error

2009-05-04 Thread Steve Teale
Jarrett Billingsley Wrote:

 On Sun, May 3, 2009 at 4:14 PM, Denis Koroskin 2kor...@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.

;=(



Re: Throwable, Exception, and Error

2009-05-03 Thread Frank Benoit
Walter Bright schrieb:
 Steve Teale wrote:
 If I want a catch-all catch, which should I be using.

 Out of habit I use Exception, but if I do that then I will miss some
 things thrown from Phobos.

 Is it the intention that Throwable be used for this purpose?

 
 Yes.

Why is it possible to throw an Object?
I think throw and catch should be restricted to Throwable and
derived types.


Re: Throwable, Exception, and Error

2009-05-03 Thread Denis Koroskin

On Sun, 03 May 2009 09:57:02 +0400, Frank Benoit keinfarb...@googlemail.com 
wrote:


Walter Bright schrieb:

Steve Teale wrote:

If I want a catch-all catch, which should I be using.

Out of habit I use Exception, but if I do that then I will miss some
things thrown from Phobos.

Is it the intention that Throwable be used for this purpose?



Yes.


Why is it possible to throw an Object?
I think throw and catch should be restricted to Throwable and
derived types.


This is going to be addressed, so no worries.



Re: Throwable, Exception, and Error

2009-05-03 Thread Christopher Wright

Frank Benoit wrote:

Walter Bright schrieb:

Steve Teale wrote:

If I want a catch-all catch, which should I be using.

Out of habit I use Exception, but if I do that then I will miss some
things thrown from Phobos.

Is it the intention that Throwable be used for this purpose?


Yes.


Why is it possible to throw an Object?
I think throw and catch should be restricted to Throwable and
derived types.


Because Phobos did not contain Throwable until the advent of druntime. 
This was sufficiently recent that Walter has not yet modified the 
compiler accordingly, or, most likely, noticed or decided to do so.


Re: Throwable, Exception, and Error

2009-05-03 Thread Steve Teale
Christopher Wright Wrote:

 Frank Benoit wrote:
  Walter Bright schrieb:
  Steve Teale wrote:
  If I want a catch-all catch, which should I be using.
 
  Out of habit I use Exception, but if I do that then I will miss some
  things thrown from Phobos.
 
  Is it the intention that Throwable be used for this purpose?
 
  Yes.
  
  Why is it possible to throw an Object?
  I think throw and catch should be restricted to Throwable and
  derived types.
 
 Because Phobos did not contain Throwable until the advent of druntime. 
 This was sufficiently recent that Walter has not yet modified the 
 compiler accordingly, or, most likely, noticed or decided to do so.

When that is done, and the object being thrown has been checked out, would it 
be a good idea for the compiler to automatically populate the file and line 
members of the Throwable?



Re: Throwable, Exception, and Error

2009-05-03 Thread Christopher Wright

Steve Teale wrote:

Christopher Wright Wrote:


Frank Benoit wrote:

Walter Bright schrieb:

Steve Teale wrote:

If I want a catch-all catch, which should I be using.

Out of habit I use Exception, but if I do that then I will miss some
things thrown from Phobos.

Is it the intention that Throwable be used for this purpose?


Yes.

Why is it possible to throw an Object?
I think throw and catch should be restricted to Throwable and
derived types.
Because Phobos did not contain Throwable until the advent of druntime. 
This was sufficiently recent that Walter has not yet modified the 
compiler accordingly, or, most likely, noticed or decided to do so.


When that is done, and the object being thrown has been checked out, would it 
be a good idea for the compiler to automatically populate the file and line 
members of the Throwable?


Hrm. You want to assign the file and line numbers where the exception is 
thrown, but not if you are rethrowing the exception.


The runtime has something like _d_throw; if the signature included 
__FILE__ and __LINE__ for the throw expression, that should work.


Re: Throwable, Exception, and Error

2009-05-03 Thread Steve Teale
Christopher Wright Wrote:

 Steve Teale wrote:
  Christopher Wright Wrote:
  
  Frank Benoit wrote:
  Walter Bright schrieb:
  Steve Teale wrote:
  If I want a catch-all catch, which should I be using.
 
  Out of habit I use Exception, but if I do that then I will miss some
  things thrown from Phobos.
 
  Is it the intention that Throwable be used for this purpose?
 
  Yes.
  Why is it possible to throw an Object?
  I think throw and catch should be restricted to Throwable and
  derived types.
  Because Phobos did not contain Throwable until the advent of druntime. 
  This was sufficiently recent that Walter has not yet modified the 
  compiler accordingly, or, most likely, noticed or decided to do so.
  
  When that is done, and the object being thrown has been checked out, would 
  it be a good idea for the compiler to automatically populate the file and 
  line members of the Throwable?
 
 Hrm. You want to assign the file and line numbers where the exception is 
 thrown, but not if you are rethrowing the exception.
 
 The runtime has something like _d_throw; if the signature included 
 __FILE__ and __LINE__ for the throw expression, that should work.

OK, I did not think it through, but it would save a lot of typing grief;

 ... , __FILE__, __LINE__);

every time you wanted to be careful.



Re: Throwable, Exception, and Error

2009-05-03 Thread Denis Koroskin

On Sun, 03 May 2009 22:12:00 +0400, Steve Teale steve.te...@britseyeview.com 
wrote:


Christopher Wright Wrote:


Steve Teale wrote:
 Christopher Wright Wrote:

 Frank Benoit wrote:
 Walter Bright schrieb:
 Steve Teale wrote:
 If I want a catch-all catch, which should I be using.

 Out of habit I use Exception, but if I do that then I will miss  
some

 things thrown from Phobos.

 Is it the intention that Throwable be used for this purpose?

 Yes.
 Why is it possible to throw an Object?
 I think throw and catch should be restricted to Throwable and
 derived types.
 Because Phobos did not contain Throwable until the advent of  
druntime.

 This was sufficiently recent that Walter has not yet modified the
 compiler accordingly, or, most likely, noticed or decided to do so.

 When that is done, and the object being thrown has been checked out,  
would it be a good idea for the compiler to automatically populate the  
file and line members of the Throwable?


Hrm. You want to assign the file and line numbers where the exception is
thrown, but not if you are rethrowing the exception.

The runtime has something like _d_throw; if the signature included
__FILE__ and __LINE__ for the throw expression, that should work.


OK, I did not think it through, but it would save a lot of typing grief;

 ... , __FILE__, __LINE__);

every time you wanted to be careful.



PHP allows function definition like this:

void foo(Args args, string fileName = __FILE__, int line = __LINE__)
{
  // do stuff
}

You call it like this: foo(args); and correct file name and line number is 
passed, i.e. default arguments are evaluated at call site. Can we use the same trick for 
exceptions? Here is an example:

class Throwable
{
   this(string fileName = __FILE__, int line = __LINE__) { ... }
}

class Exception : Throwable
{
   this(string reason, string fileName = __FILE__, int line = __LINE__)
   {
   super(fileName, line);
   // ...
   }
}



Re: Throwable, Exception, and Error

2009-05-03 Thread Jarrett Billingsley
On Sun, May 3, 2009 at 4:14 PM, Denis Koroskin 2kor...@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.


Re: Throwable, Exception, and Error

2009-05-03 Thread Jarrett Billingsley
On Sun, May 3, 2009 at 4:36 PM, Jarrett Billingsley
jarrett.billings...@gmail.com wrote:

        catch(RangeError e)

eh that's supposed to be Exception


Re: Throwable, Exception, and Error

2009-05-03 Thread Andrei Alexandrescu

Jarrett Billingsley wrote:

On Sun, May 3, 2009 at 4:14 PM, Denis Koroskin 2kor...@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.


That's intentional and is used in enforce() and a couple other places in 
Phobos.


Andrei


Re: Throwable, Exception, and Error

2009-05-03 Thread Jarrett Billingsley
On Sun, May 3, 2009 at 5:07 PM, Andrei Alexandrescu
seewebsiteforem...@erdani.org wrote:

 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.

 That's intentional and is used in enforce() and a couple other places in
 Phobos.

That's what I figured.  It wasn't until I looked at the source to
std.contracts that I found out about it, though, and I doubt many
other people know about it.  The only mention about it in the
changelog is for 2.013, where it says Changed __FILE__ and __LINE__
so they work as parameter default initializers but that doesn't..
well, doesn't say a lot :P

In any case, I doubt the bug is intentional.


Throwable, Exception, and Error

2009-05-02 Thread Steve Teale
If I want a catch-all catch, which should I be using.

Out of habit I use Exception, but if I do that then I will miss some things 
thrown from Phobos.

Is it the intention that Throwable be used for this purpose?



Re: Throwable, Exception, and Error

2009-05-02 Thread Walter Bright

Steve Teale wrote:

If I want a catch-all catch, which should I be using.

Out of habit I use Exception, but if I do that then I will miss some things 
thrown from Phobos.

Is it the intention that Throwable be used for this purpose?



Yes.