Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
February 20, 2013 collectException range violation | ||||
---|---|---|---|---|
| ||||
Is this example from the docs still meant to work? int[] a = new int[3]; int b; assert(collectException(a[4], b)); |
February 20, 2013 Re: collectException range violation | ||||
---|---|---|---|---|
| ||||
Posted in reply to cal | 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 |
February 20, 2013 Re: collectException range violation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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!
|
February 20, 2013 Re: collectException range violation | ||||
---|---|---|---|---|
| ||||
Posted in reply to cal | 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.
}
|
February 20, 2013 Re: collectException range violation | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | 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 :).
|
Copyright © 1999-2021 by the D Language Foundation