Exceptions names list
Ali Çehreli
acehreli at yahoo.com
Tue Nov 16 09:29:34 UTC 2021
On 11/16/21 12:30 AM, bauss wrote:
> On Monday, 15 November 2021 at 22:44:35 UTC, Ali Çehreli wrote:
>> On 11/15/21 2:28 PM, pascal111 wrote:
>> > I want to know where the list of exceptions names that
>> "throw" statement
>> > uses.
>>
>> Such a list is not very useful. Normally all we need is Exception and
>> occasionally one or two other specific types.
>>
>
> It's actually very useful when you want to throw exceptions yourself,
That's very different from how I use exceptions perhaps because mine
have been command line tools. I would love to learn more about how
others use exceptions.
The existing exception type that I reuse is Exception :), which I throw
indirectly by enforce:
enforce(isOk, format!"There is a problem with %s"(x));
I even convert a ConvException that I catch to Exception with the same
method just to give a little bit of more
catch (ConvException ex) {
enforce(false,
format!"Failed to parse '%s' as blah: %s"(y, ex.msg));
}
That shows how lazy I am. :)
> because instead of writing your own custom exception then you could use
> an existing implementation.
Unless there are standardized exceptions, that feels weird to me because
such reused exceptions may have constructor parameters which may not
make sense where I throw that exception.
Luckily, there is basicExceptionCtors, which trivializes implementing
exception types anyway. For example, I copied the following from
/usr/include/dmd/phobos/std/conv.d:
class ConvException : Exception
{
import std.exception : basicExceptionCtors;
///
mixin basicExceptionCtors;
}
Further, it doesn't feel right to throw e.g. std.conv.ConvException from
my foo.bar module. The cases where another module's exception fitting my
use closely feels so rare that I wouldn't even think about reaching for
an existing exception of another module or a library.
I don't know... That's my comfortable and lazy use of exceptions. :)
Ali
More information about the Digitalmars-d-learn
mailing list