Can we make Throwable an interface?

Dmitry Olshansky via Digitalmars-d digitalmars-d at puremagic.com
Wed Dec 10 12:06:18 PST 2014


10-Dec-2014 11:24, Kagamin пишет:
> On Tuesday, 9 December 2014 at 17:06:45 UTC, Dmitry Olshansky wrote:
>> 1. enums are hard to extend for std lib, and absolutely impossible by
>> 3rd party libraries.
>
> What's the problem?

That you miss the point of standard exception hierarchy - that is client 
code shall not be concerned with 3-rd party exceptions of every library 
as long as 3rd parties use common hierarchy(s).

A big problem is we can't catch based on enum or would have to rethrow.

Another problem is that one hierarchy is too rigid and exceptions can be 
viewed from different angles (implementation-wise like WindowsException 
and semantic categories like PermissionException).

> When you add new functionality to std lib, you add
> an enum entry in the same pull request.

Aye. And every program that had final switch on it fails. There is too 
many opportunities to break code with that. In contrast with interfaces 
one explicitly has open set of possible derived interfaces.

> 3rd party libraries define their
> specific exceptions and enums.

Which is utter failure. If every MyFooBar library defines MyFooBar 
exception then the whole point of standard exceptions is moot. We 
degrade back to specific per library error codes, with a benefit that 
they are transported across stack frames.

The point is to _catch_ based on common failure causes the same way 
across _any_ libraries. The 3rd parties are then free to provide more 
specific causes of say OsException or FormatException etc. which may 
then find their way into standard.

So that all arithmetic libraries inherit Underflow and Overflow as 
needed in their custom exceptions, or even just use anonymous class and 
be done.

This allows extension while keeping client's code simple. Unlike DIP33 
where one cannot make FormatException grow custom enum flag for his own 
library _only_ thus requiring client code to _always_ deal with your 
custom exception explicitly.

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list