www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - scope(failure): get exception

reply Justin Whear <justin economicmodeling.com> writes:
Is there a way to get the current exception inside a scope(failure)?  I 
have a try..catch around my main loop which simply logs the caught 
exception and rethrows it.  I'd like to replace this with a simple scope
(failure) but I haven't found any way to access the exception causing the 
unwinding.
Oct 26 2012
parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Friday, October 26, 2012 19:53:04 Justin Whear wrote:
 Is there a way to get the current exception inside a scope(failure)? I
 have a try..catch around my main loop which simply logs the caught
 exception and rethrows it. I'd like to replace this with a simple scope
 (failure) but I haven't found any way to access the exception causing the
 unwinding.
If you want to see the exception, you must catch it. scope statements do not provide access to exceptions. So, scope works great as long as you don't need access to the exception, but if you do, you need to use try-catch blocks. - Jonathan M Davis
Oct 26 2012
parent reply Justin Whear <justin economicmodeling.com> writes:
On Fri, 26 Oct 2012 17:33:48 -0400, Jonathan M Davis wrote:

 On Friday, October 26, 2012 19:53:04 Justin Whear wrote:
 Is there a way to get the current exception inside a scope(failure)? I
 have a try..catch around my main loop which simply logs the caught
 exception and rethrows it. I'd like to replace this with a simple scope
 (failure) but I haven't found any way to access the exception causing
 the unwinding.
If you want to see the exception, you must catch it. scope statements do not provide access to exceptions. So, scope works great as long as you don't need access to the exception, but if you do, you need to use try-catch blocks. - Jonathan M Davis
My understanding is that scope(failure) simply lowers to the catch block of a try..catch, so there's no implementation obstacle to making the exception available. Are there known syntax or correctness obstacles to allowing something like this: scope(failure)(Exception ex) writeln(ex.msg);
Oct 26 2012
parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Friday, October 26, 2012 21:37:00 Justin Whear wrote:
 My understanding is that scope(failure) simply lowers to the catch block
 of a try..catch, so there's no implementation obstacle to making the
 exception available. Are there known syntax or correctness obstacles to
 allowing something like this:
 
 scope(failure)(Exception ex) writeln(ex.msg);
Yes. It lowers to a try-catch block, but that's effectively an implementation detail. As it stands, technically speaking, a compiler could probably implement it without any lowering whatsoever (I don't think that the spec says anything about how it's implemented). But even if the compiler has to use lowering, the main problem with your suggestion is that in complicates how scope statements work, since then it's only going to be run if the exception type being caught matches what was being thrown, whereas right now scope(failure) statements run on all exceptions regardless. And scope statements aren't really meant for exception handling. Rather, they're intended for providing a clean means of making code exception-safe. So, I suspect that what you suggest would be rejected, but I don't know. You can certainly create an enhancement request for it if you want to: http://d.puremagic.com/issues - Jonathan M Davis
Oct 26 2012
parent reply Jacob Carlborg <doob me.com> writes:
On 2012-10-26 23:56, Jonathan M Davis wrote:

 Yes. It lowers to a try-catch block, but that's effectively an implementation
 detail. As it stands, technically speaking, a compiler could probably
 implement it without any lowering whatsoever (I don't think that the spec says
 anything about how it's implemented). But even if the compiler has to use
 lowering, the main problem with your suggestion is that in complicates how
 scope statements work, since then it's only going to be run if the exception
 type being caught matches what was being thrown, whereas right now
 scope(failure) statements run on all exceptions regardless. And scope
 statements aren't really meant for exception handling. Rather, they're
 intended for providing a clean means of making code exception-safe. So, I
 suspect that what you suggest would be rejected, but I don't know. You can
 certainly create an enhancement request for it if you want to:
What about allowing catch-statements without a try-statement, something like this: void foo () { // some code ... catch (Exception e) { } } This would be the same as the whole function would be wrapped in a try-statement. It's a quite handy feature that's available in Ruby. -- /Jacob Carlborg
Oct 28 2012
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, October 28, 2012 13:08:50 Jacob Carlborg wrote:
 What about allowing catch-statements without a try-statement, something
 like this:
 
 void foo ()
 {
      // some code ...
 
      catch (Exception e)
      {
 
      }
 }
 
 This would be the same as the whole function would be wrapped in a
 try-statement. It's a quite handy feature that's available in Ruby.
So, you save one set of braces? I don't see how that really buys you much. - Jonathan M Davis
Oct 28 2012
parent reply Jacob Carlborg <doob me.com> writes:
On 2012-10-28 13:20, Jonathan M Davis wrote:

 So, you save one set of braces? I don't see how that really buys you much.
Yes, and the "try" keyword. It would basically be the same as allowing to get the exception in scope(failure) but the catch-statement already supports this. -- /Jacob Carlborg
Oct 29 2012
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, October 29, 2012 10:30:35 Jacob Carlborg wrote:
 On 2012-10-28 13:20, Jonathan M Davis wrote:
 So, you save one set of braces? I don't see how that really buys you much.
Yes, and the "try" keyword. It would basically be the same as allowing to get the exception in scope(failure) but the catch-statement already supports this.
Except that the place that scope statements go in the code is completely different from where catch statements go. catch statements go at the end whereas scope statements go in the middle or even the beginning so that what you're doing in there can be close to code that corresponds to it (e.g. you can have the code for releasing a resource right after the code for aquiring it rather than having it in a complete separate part of the code. What you typically do with scope statements and try-catch statements is often fundamentally different. I can understand wanting to be able to have access to the exception that's flying by in a scope statement, but I really don't see how saving a try and couple of braces adds much. Certainly, the two are completely different in terms of what they buy you. - Jonathan M Davis
Oct 29 2012
parent Jacob Carlborg <doob me.com> writes:
On 2012-10-29 11:58, Jonathan M Davis wrote:

 Except that the place that scope statements go in the code is completely
 different from where catch statements go. catch statements go at the end
 whereas scope statements go in the middle or even the beginning so that what
 you're doing in there can be close to code that corresponds to it (e.g. you
 can have the code for releasing a resource right after the code for aquiring
 it rather than having it in a complete separate part of the code. What you
 typically do with scope  statements and try-catch statements is often
 fundamentally different.

 I can understand wanting to be able to have access to the exception that's
 flying by in a scope statement, but I really don't see how saving a try and
 couple of braces adds much. Certainly, the two are completely different in
 terms of what they buy you.
I've used it a couple of times in Ruby, only in very small methods. It's not a deal breaker, it's a "nice to have" feature. -- /Jacob Carlborg
Oct 29 2012