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.
}