www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - collectException range violation

reply "cal" <callumenator gmail.com> writes:
Is this example from the docs still meant to work?

int[] a = new int[3];
int b;
assert(collectException(a[4], b));
Feb 19 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 02/19/2013 04:38 PM, cal wrote:
 Is this example from the docs still meant to work?

 int[] a = new int[3];
 int b;
 assert(collectException(a[4], b));
The example is wrong. a[4] throws an Error (not Exception) but collectException catches Exception by default. Additionally, the example is doing something that is recommended against: Catching Error or a descendent of it. Still, it can be told to catch by Error: assert(collectException!Error(a[4], b)); Ali
Feb 19 2013
parent reply "cal" <callumenator gmail.com> writes:
On Wednesday, 20 February 2013 at 01:19:54 UTC, Ali Çehreli wrote:
 The example is wrong. a[4] throws an Error (not Exception) but 
 collectException catches Exception by default.

 Additionally, the example is doing something that is 
 recommended against: Catching Error or a descendent of it.

 Still, it can be told to catch by Error:

     assert(collectException!Error(a[4], b));

 Ali
Ah right I didn't realize it could be made to collect an Error. Thanks!
Feb 19 2013
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Wednesday, 20 February 2013 at 01:32:10 UTC, cal wrote:
 On Wednesday, 20 February 2013 at 01:19:54 UTC, Ali Çehreli 
 wrote:
 The example is wrong. a[4] throws an Error (not Exception) but 
 collectException catches Exception by default.

 Additionally, the example is doing something that is 
 recommended against: Catching Error or a descendent of it.

 Still, it can be told to catch by Error:

    assert(collectException!Error(a[4], b));

 Ali
Ah right I didn't realize it could be made to collect an Error. Thanks!
Note that an Error is not an Exception, and an Exception is not an Error. Both, however, are Throwable's. If you want to catch an *anything*, then catch a Throwable. As already mentioned though, catching an Error is not something you usually do, as their existence implies an already catastrophic state. At best, they can be used to semi-gracefully die. Ergo, catching a Throwable is an even worst idea than catching an exception, as both shouldn't be treated the same way. EG: import std.stdio; import std.c.stdlib; void foo(){???} void main() { try{ foo(); } catch(Exception e) { stderr.writeln("an exception was thrown. No biggy."); //Do nothing about it. } catch(Error e) { stderr.writen("A catastrophic error occurred. The program must close."); exit(); } //Continue code here. }
Feb 19 2013
parent "cal" <callumenator gmail.com> writes:
On Wednesday, 20 February 2013 at 07:54:19 UTC, monarch_dodra 
wrote:
 Note that an Error is not an Exception, and an Exception is not 
 an Error. Both, however, are Throwable's. If you want to catch 
 an *anything*, then catch a Throwable.

 As already mentioned though, catching an Error is not something 
 you usually do, as their existence implies an already 
 catastrophic state. At best, they can be used to 
 semi-gracefully die.
Yeah I am trying to catch a range violations thrown inside D Dlls, which if not trapped somehow manually, crash very ungracefully :).
Feb 20 2013