digitalmars.D.learn - collectException range violation
- cal (4/4) Feb 19 2013 Is this example from the docs still meant to work?
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (8/12) Feb 19 2013 The example is wrong. a[4] throws an Error (not Exception) but
- cal (3/10) Feb 19 2013 Ah right I didn't realize it could be made to collect an Error.
- monarch_dodra (31/46) Feb 19 2013 Note that an Error is not an Exception, and an Exception is not
- cal (5/12) Feb 20 2013 Yeah I am trying to catch a range violations thrown inside D
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
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
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)); AliAh right I didn't realize it could be made to collect an Error. Thanks!
Feb 19 2013
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: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. }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)); AliAh right I didn't realize it could be made to collect an Error. Thanks!
Feb 19 2013
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